vue 打包部署子目录后 elementUI icon 图标不显示

问题 部署后element-icon 请求资源404

解决

修改 vue.config.js (添加前缀路径)

vue.config.js完整代码

'use strict'
const path = require('path')
const _publicPath = '/Pledged/'
const defaultSettings = require('./src/settings.js')

function resolve(dir) {
  return path.join(__dirname, dir)
}

const names = defaultSettings.title // page title
const { name } = require('./package');

const port = 3004 // dev port

// 所有配置项的说明: https://cli.vuejs.org/config/
module.exports = {
  /**
   * 如果您计划在子路径下部署站点,则需要设置publicPath,
   * 在大多数情况下请使用'/' !!!
   * https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/Pledged/',
  outputDir: 'dist',
  assetsDir: 'static',
  transpileDependencies: ['@antv/g6'], // antv.g6
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    // 关闭主机检查,使微应用可以被 fetch
    disableHostCheck: true,
    headers: {
      'Access-Control-Allow-Origin': '*',
    },
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: 'http://114.113.127.115:9009/prod-api/', // 阿里云地址
        changeOrigin: true,
        pathRewrite: {
          ['^' + process.env.VUE_APP_BASE_API]: ''
        }
      }
    }
  },
  configureWebpack: {
    // 在webpack的name字段中提供应用程序的标题,以便可以在index.html中访问它以插入正确的标题。
    name: names,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    output: {
      library: 'inside-grade2',
      libraryTarget: 'umd', // 把微应用打包成 umd 库格式
      jsonpFunction: 'webpackJsonp_inside-grade2',
    },
  },
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
//解决elementUI icon部署不显示
    config.module
      .rule('fonts')
      .use('url-loader')
      .loader('url-loader')
      .tap(options => {
          options['limit'] = 4096
          options['publicPath'] = _publicPath	//添加路径
          return options;
      })
    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap(options => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'development',
        config => config.devtool('cheap-source-map')
      )

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
              // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          config.optimization.runtimeChunk('single')
        }
      )
  }
}

你可能感兴趣的:(elementui,vue.js,前端)