【问题标题】:How to display images from an array in vuepress or vuejs?如何在 vuepress 或 vuejs 中显示数组中的图像?
【发布时间】:2021-10-14 19:55:51
【问题描述】:

我有一个如下所示的数组

  questions: [
    {
      question: "How do you say 'My Car' in Malayalam",
      answers: {
        a: "a) Ente Car",
        b: "b) Ninte/Ningalude Car",
        c: "c) Onte Car",
        d: "d) Aarudeyo Car",
      },
      images: "@alias/vallamkali.jpg",
      correctAnswer: "a",
    },
    {
      question: "How do you say 'your Car' in Malayalam",
      answers: {
        a: "a) Onte Car",
        b: "b) Aarudeyo Car",
        c: "c) Ninte/Ningalude Car",
        d: "d) Ente Car",
      },
      images: "@alias/il_leki.png",
      correctAnswer: "c",
    },
    {
      question: "How do you say 'our car' in Malayalam",
      answers: {
        a: "a) Achante Car",
        b: "b) Ninte/Ningalude Car",
        c: "c) Ente Car",
        d: "d) Nammalude/Njangalude Car",
      },
      images: "@alias/isthapetta_doubt.jpg",
      correctAnswer: "d",
    },
  ],

但是当我尝试使用下面的代码进行打印时

        <div v-if="index < count">
        
        <p>{{ questions[index]['question']}}</p>
        <p>{{ questions[index]['images']}}</p

        </div>

只有问题生成正确但图像显示不正确,只有位置打印如下,并以蓝色突出显示。请帮忙。

【问题讨论】:

    标签: vue.js vuepress


    【解决方案1】:

    我没有使用函数调用。 我直接在img 标签本身中使用了require 关键字并且它起作用了。

    <img :src="require(`@alias/${questions[index]['images']}`)" alt="No image here too" />
    

    @Nikola Pavicevic - 感谢您帮助我朝这个方向思考!

    【讨论】:

      【解决方案2】:

      您不能在 p 标签中显示图像 您需要制作方法或计算属性(假设 images = il_leki.png):

      methods: {
        getImg(img) {
          return require(`@alias/${img}`);
        }
      }
      

      然后在模板中调用 img 标签中的该方法(而不是 p 标签)传递 img :

      <img :src="getImg(questions[index]['images']) />
      

      【讨论】:

      • aliased with mapping '@alias': '/home/robo/git/vuepress-easymalayalam/imgs' to '/home/robo/git/vuepress-easymalayalam/imgs/${img}' 尝试使用vuepress build 构建时出现以下错误。构建失败。不知何故,别名在这里没有被正确转换。我已经正确设置了图片路径 -> images: il_leki.png
      猜你喜欢
      • 2020-06-29
      • 2021-10-12
      • 2020-05-08
      • 2020-03-15
      • 2020-02-01
      • 2018-08-02
      • 2019-09-24
      • 2019-09-13
      • 1970-01-01
      相关资源
      最近更新 更多