vue cli4配置动态地址

有些资源地址 vue打包之后如果修改需要重新打包,这就很烦,有很多 vue cli2 的参考文档,刚好配置出了 vue cli4 ,在这里记录一下

  1. 安装 generate-asset-webpack-plugin
yarn add  generate-asset-webpack-plugin
  1. vue.config.js 中配置 generate-asset-webpack-plugin

在这里插入图片描述

const GenerateAssetPlugin = require("generate-asset-webpack-plugin"); //引入插件
const serverConfig = require("./fileUrl.json");

const createJson = function (compilation) {
    return JSON.stringify(serverConfig);
};

module.exports = {
 ...
    configureWebpack: {
        plugins: [
            new GenerateAssetPlugin({
                filename: 'fileUrl.json',
                fn: (compilation, cb) => {
                    cb(null, createJson(compilation));
                }
            })
        ]
    },
     ...
    }
  1. 在main.js 中加载 fileUrl.json,并且配置为全局参数,进行调用
Vue.prototype.getConfigJson = function () {
  axios.get(fileUrl.json').then((result)=>{
    console.log(result);
    Vue.prototype.fileJson =result.data;//设置成Vue的全局属性
    new Vue({
      router,
      store,
      render: h => h(App)
    }).$mount("#app");
  }).catch((error)=>{
    console.log(error)
  })
}
Vue.prototype.getConfigJson()//调用声明的全局方法

4.使用 用 this.fileJson 即可拿到 fileUrl.json 对象,

<template>
  <div class="hello">
    ================
    <h1>{{ fileJson }}</h1>
    ================
  </div>
</template>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值