图像不在循环 Vue.js 中显示

标签 image vue.js gallery

<分区>

我正在尝试使用 v-for 循环显示 9 张不同的图像。

但是,他们没有出现。如果我在没有任何循环的情况下显示它,它就可以工作。

我正在提取正确的资源,但它仍然不会显示。

这是我的代码:

<img class="list-complete-img" src="../assets/g1.jpg" alt="" /> //This works
<div v-for="item in data.items" :key="item.id">
    <div>
        {{ item.src }} // Just to check if I am printing right
    </div>
    <img class="list-complete-img" :src="item.src" :alt="item.src" /> // This does not work
</div>

现在我得到的结果是:

This is the snippet I am receiving

这是我的data.json:

 "items": [
    { "id": "1", "src": "../assets/g1.jpg", "tags": ["all", "tag1"] },
    { "id": "2", "src": "../assets/g2.png", "tags": ["all", "tag2"] },
    { "id": "3", "src": "../assets/g3.png", "tags": ["all", "tag2"] },
    { "id": "4", "src": "../assets/g4.png", "tags": ["all", "tag1"] },
    { "id": "5", "src": "../assets/g5.png", "tags": ["all", "tag1"] },
    { "id": "6", "src": "../assets/g6.png", "tags": ["all", "tag2"] },
    { "id": "7", "src": "../assets/g7.jpg", "tags": ["all", "tag1"] },
    { "id": "8", "src": "../assets/g8.png", "tags": ["all", "tag2"] },
    { "id": "9", "src": "../assets/g9.png", "tags": ["all", "tag2"] }
  ]

编辑

到目前为止,我发现问题出在 src 上。如果我使用图像链接,它工作得很好。但是,不适用于本地镜像(仅当我在循环中使用一堆本地镜像并且在单个图像中工作得很好时)。所以,我能做的就是把文件目录放在这里。如果你们中的任何人都可以尝试在本地计算机上循环上传文件目录中的图像并张贴在这里,我会推荐。

File Directory

Inspect Element as required


已解决

它恰好需要这个语句:require,路径目录和图像名称。

<div v-for="item in items" :key="item.id">
    <div>
        {{ item.src }}
    </div>
    <img
        class="list-complete-img"
        :src="require(`../assets/${item.src}`)"
        :alt="item.src"
    />
</div>

最佳答案

这个问题与以下Evan You的引述有关,可以找到here :

Because the imagePath is only rendered by Vue at runtime, Webpack has no chance of rewriting it.

您的 JavaScript 代码需要像这样:

export default {
  name: "App",
  data: function () {
    return {
      items: [
        { id: "1", src: "logo.png", tags: ["all", "tag1"] },
        { id: "2", src: "logo.png", tags: ["all", "tag2"] },
        { id: "3", src: "logo.png", tags: ["all", "tag2"] },
        { id: "4", src: "logo.png", tags: ["all", "tag1"] },
        { id: "5", src: "logo.png", tags: ["all", "tag1"] },
        { id: "6", src: "logo.png", tags: ["all", "tag2"] },
        { id: "7", src: "logo.png", tags: ["all", "tag1"] },
        { id: "8", src: "logo.png", tags: ["all", "tag2"] },
        { id: "9", src: "logo.png", tags: ["all", "tag2"] },
      ],
    };
  },
  methods: {
    getImgUrl: function (imagePath) {
      return require('@/assets/' + imagePath);
    }
  }
};

之后,您需要通过传递文件名来调用您的getImgUrl 函数。您的 HTML 将如下所示:

<div id="app">
  <img class="list-complete-img" src="./assets/logo.png" />
  <div v-for="item in items" :key="item.id">
    <img class="list-complete-img" :src="getImgUrl(item.src)" />
  </div>
</div>

由于我无权访问您的图片,我冒昧地使用 Vue Logo 作为图片。

总而言之,上述问题必须在 Vue 编译时执行,它会获取所有 Assets 并将它们放在另一个文件夹中,如下图所示:

enter image description here

遵循此模式,您的示例也将有效。享受吧!

编辑:如果需要,您可以将静态 Assets 放在公用文件夹中。这样,您就可以为 Assets 编写静态路径。

关于图像不在循环 Vue.js 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63445526/

相关文章:

javascript - 初始化 Vue.js 时出现延迟

android - 为 Android 中的图库项目添加阴影?

android - 如何在图库 View 中像图像一样显示视频?

python - 使用 pytesseract OCR 识别图像中的文本

单元测试的图像比较算法

css - 调整背景(图像)的大小而不裁剪它

javascript - 为什么 window.location.href 在 Vue.js 中不起作用?

jQuery onResize 图像定位问题

javascript - 找不到字体,vuejs构建

javascript - extjs 数据 View 不显示图像