700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vue打包分离配置文件

vue打包分离配置文件

时间:2019-05-06 22:02:00

相关推荐

vue打包分离配置文件

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生成可修改的配置文件,可以直接修改请求路径等,不需要重新打包

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。