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就可以在相应的页面使用了。
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本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!