从setAttribute设置javascript中图像的路径

问题描述 投票:0回答:1

im试图通过setAttribute设置本地存储中图像的路径,但是尽管已经明确设置了路径,但仍会给我这个找不到图像的错误,这是我的部分代码:

if (shipLocation[0][0] == shipLocation[1][0] && i == 0) {
    var x = document.createElement("IMG");
    x.setAttribute("src", "../assets/patrolBoatHorizontalView.png"); // THIS IS THE PATH

}

错误

GEThttp://localhost:8081/assets/patrolBoatHorizontalView.png404(不是找到)

我不清楚这是否是我应该使用的方式……我使用VisualStudio编辑器将我的所有图像存储在资产中,并且请求是通过此Vuejs应用程序中的一个组件完成的

有任何建议吗?...提前谢谢!!

javascript vue.js setattribute
1个回答
0
投票

尝试一下。

if (shipLocation[0][0] == shipLocation[1][0] && i == 0) {
  var x = document.createElement("img");
  let image = require("../assets/patrolBoatHorizontalView.png");
  x.setAttribute("src", image); 
}

© www.soinside.com 2019 - 2024. All rights reserved.