【问题标题】:<img src=""> is generating different path after build in vuejs/webpack<img src=""> 在 vuejs/webpack 中构建后生成不同的路径
【发布时间】:2023-04-09 08:47:02
【问题描述】:

我是 Vuejs 的新手。我正在一个项目中工作,我必须在 Vue 组件中显示图像。在我的组件中,我正在显示这样的图像:

&lt;img src="./../../images/back-arrow.png"&gt;

然后构建后不显示。当我检查我的代码时,它显示不同的地址,如下所示:

&lt;img src="/js/back-arrow.png?c4a414352d997e4618088074e1da917a"&gt;

我的文件夹结构是:

  • 资产
    • 组件
      • VueTemplate.vue //这是我展示图片的地方
    • 图片
      • 后退箭头.png
    • js //npm 运行构建位置

我不知道为什么它会改变,有人可以帮助我吗? TIA

我的 webpack 配置:

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: './admin/assets/main.js',
  output: {
    path: path.resolve(__dirname, 'admin/js'),
    publicPath: '/js/',
    filename: 'graphs-lite-admin.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': [
              'vue-style-loader',
              'css-loader',
              'sass-loader'
            ],
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        exclude: /node_modules/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]',
          outputPath: 'images/'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    // new webpack.optimize.UglifyJsPlugin({
    //   sourceMap: true,
    //   compress: {
    //     warnings: false
    //   }
    // }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

【问题讨论】:

    标签: webpack vuejs2


    【解决方案1】:

    如果您使用 vue-cli 3.0 创建项目,则可以使用 filenameHashing 选项将其删除。

    在你的项目根目录上创建vue.config.js 文件,然后写这个。

    module.exports = {
      filenameHashing: false
    }
    

    查看此链接:https://cli.vuejs.org/config/#filenamehashing

    或者,如果您使用的是 webpack 配置文件,请从输出中删除 [hash]

    output: {
      filename: '[name].[hash].bundle.js'
    }
    

    https://webpack.js.org/configuration/output/#output-filename

    【讨论】:

      【解决方案2】:

      我找到了一个临时解决方案。问题出在 webpack 中。我不得不使用装载机。我使用了 url-loader。

       {
          test: /\.(png|jpg|gif|svg)$/,
          loader: 'url-loader',  //adding this line solved my problem
          options: {
            name: '[name].[ext]?[hash]'
          }
        }
      

      【讨论】:

      • url-loader 将我的图像转换为 base64,这是我不想要的。它没有访问直接资产。
      猜你喜欢
      • 2020-07-20
      • 2021-02-12
      • 2020-08-17
      • 2021-02-19
      • 2015-04-06
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多