Vue打包后,訪問(wèn)路徑可配置

首先,在static下,創(chuàng)建config.json文件
config.json代碼如下:
//config.json
 {
    "BASE_URL": "http://localhost:8087"
}
    

然后在main.js中,加入如下代碼:

let startApp = function () {
  axios.get('./static/config.json').then((res) => {
   Vue.prototype.BASE_URL = res.data.BASE_URL;//將BASE_URL掛載在Vue原型
    new Vue({
      el: '#app',
      router,
      store,
      components: { App },
      template: '<App/>',
    })
  })
}
 
startApp()

同時(shí)修改config目錄下的index.js文件中的如下內(nèi)容:

 build: {
    // Template for index.html
    index: path.resolve(__dirname, '../dist/'+ projectName +'/index.html'),

    // Paths
    assetsRoot: path.resolve(__dirname, '../dist/' + projectName),
    assetsSubDirectory: 'static',
    assetsPublicPath: './',

    /**
     * Source Maps
     */

    productionSourceMap: false,
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',
     // Gzip off by default as many popular static hosts such as
    // Surge or Netlify already gzip all static assets for you.
    // Before setting to `true`, make sure to:
    // npm install --save-dev compression-webpack-plugin
    productionGzip: false,
    productionGzipExtensions: ['js', 'css'],

    // Run the build command with an extra argument to
    // View the bundle analyzer report after build finishes:
    // `npm run build --report`
    // Set to `true` or `false` to always turn it on or off
    bundleAnalyzerReport: process.env.npm_config_report
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容