解決vue-cli構建後的index.html找不到圖標和引用的js、css文件路徑錯誤的問題

1、config/index.js:

module.exports = {
  build: {
    // Paths
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: './',
  }
}

將上面的assetsPublicPath屬性設置爲“./”

2、build/utils.js:

  function generateLoaders (loader, loaderOptions) {
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader',
        publicPath: '/' //add
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

在上面的fallback屬性後面添加publicPath屬性,並將其屬性值設置“/”。

這樣設置後重新build,將dist下面所有的文件和文件夾放到服務器上後通過“/”訪問即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章