在開發中碰到一個問題,如果公共配置寫在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
