vue-cli3.x
添加一个serverConfig.json
{"AppId": "2982b7ca5a","token": "0F33CFBA-B4E0-4A9C-A54B-2B2DABEE7B15","baseUrl": ""}
添加一个vue.config.js文件
var GenerateAssetPlugin = require('generate-asset-webpack-plugin');const serverConfig = require('./serverConfig.json') // 引入配置文件const createServerConfig = function(compilation) {return JSON.stringify(serverConfig);};module.exports = {configureWebpack: {plugins: [new GenerateAssetPlugin({filename: 'serverConfig.json',fn: (compilation, cb) => {cb(null, createServerConfig(compilation));},extraFiles: []})]}};
在main.js中
Vue.prototype.getConfigJson = function() {Vue.prototype.$axios.get('serverConfig.json').then((result) => {// console.log(result);Vue.prototype.baseConfig = result;mit('token', result.authorizeKey);// Vue.prototype.baseUrl = result.data.baseUrl; //设置成Vue的全局属性new Vue({router,store,render: h => h(App)}).$mount('#app')}).catch((error) => {console.log(error)})}Vue.prototype.getConfigJson() //调用声明的全局方法
yarn run build生成可修改的配置文件,可以直接修改请求路径等,不需要重新打包