Webpack:找不到404图片(已解决)

时间:2020-09-27 09:42:04

标签: javascript html webpack webpack-file-loader

如果我在html文件中设置了图像src,则可以获取该图像。但是当我使用脚本文件设置图像src时,控制台会一直说找不到图像(404)。这是为什么?抱歉,这是我第一次使用webpack。

文件夹和文件:

merchantid = event['merchantEmail']

html:

-dist(This folder is generated by webpack)
  -bundle.js
  -bundle.js.map
  -index.html
  -cat.jpg
-images
  -cat.jpg
-node_modules
-index.html
-index.js
-package.json
-webpack.dev.js
-webpack.prod.js

index.js:

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
  </head>
  <body>
      <h1>Hello world..</h1>
      <img class="image" src="#" alt="#">
  </body>
</html>

webpack.dev.js:

const imageEl = document.querySelector('.image');
imageEl.src = './images/cat.jpg'; <--- If I manually put this path in the html image element src, 
                                          the image will show up but when I set it in this file the image 
                                          is not found.

console.log(imageEl);

(webpack.prod.js在没有devServer的情况下是相同的)

0 个答案:

没有答案
相关问题