vue配置文件不打包

在开发中碰到一个问题,如果公共配置写在src里面会被打包,无法做到可读性可以随时更改配置,所以只能写在static文件夹下,那么就实现一个公共配置文件吧。

在static文件夹下添加一个配置文件

const httpUrl = 'http://190.168.1.1:18003/api'

function errorMethod(error, obj) {
  console.log(error)
  if (typeof (error.response) === 'undefined') {
    obj.$message({ message: '网络异常,请稍后再试...', type: 'error' })
    return
  }
  if (error.response.status === 403) {
    obj.$router.push('/')
  } else {
    obj.$message({ message: '网络异常,请稍后再试...', type: 'error' })
  }
}
export default {
  httpUrl,
  errorMethod
}

在main.js文件中添加引用

import config from '../static/config'
vue.prototype.config1 = config

资源搜索网站大全 https://www.renrenfan.com.cn 广州VI设计公司https://www.houdianzi.com

就可以在相应的页面使用了。

this.config1.httpUrl

然鹅。。。上面的这种操作并没有卵用,只是文件不打包,但是实际上还是打包进去了,无论怎么改外面这个包都无效。

既然import引用都会将文件打包,那么就采用非import方式引用,也就是最原始的引入js文件方式。

1、在static文件夹下创建文件common.js

var common = {
  httpUrl: 'http://192.168.1.1:18003/project',
  pollTime: 10000,
  errorMethod: function(error, obj) {
    console.log(error)
    if (typeof (error.response) === 'undefined') {
      obj.$message({ message: '网络异常,请稍后再试...', type: 'error' })
      return
    }
    if (error.response.status === 403) {
      obj.$router.push('/')
    } else {
      obj.$message({ message: '网络异常,请稍后再试...', type: 'error' })
    }
  }
}

2、在你的vue-cli根目录的index.html文件中添加你的这个js文件引用。

<script src="static/common.js"></script>

3、就按照这种引入方式来调用即可拿到值。

common.httpUrl