简体   繁体   English

在 VueJS 中使用'require'显示图片时如何保存图片链接?

[英]How to save image link in when using 'require' to display image in VueJS?

I am trying to create a page where I show Pizza items with their images.我正在尝试创建一个页面,在其中显示带有图像的 Pizza 项目。 The Pizza object is saved in the DB with a field imgUrl .披萨 object 保存在数据库中,字段imgUrl The image is not getting displayed but I see the alt text that I provided but I can see in the console that the image link is correct.图像没有显示,但我看到了我提供的alt文本,但我可以在控制台中看到图像链接是正确的。

Right now, the imgUrl field in the database has data like ../assets/images/pizza.jpg .现在,数据库中的 imgUrl 字段有类似../assets/images/pizza.jpg的数据。 Should I instead save require(../assets/images/pizza.jpg) in the db.我应该改为在数据库中保存require(../assets/images/pizza.jpg) That looks weird.这看起来很奇怪。 Here is the code, please look at the mounted method.下面是代码,请看mounted方法。

<template>
  <div>
    <div class="class">
      <span><h1>All you can eat Menu.</h1></span>
      <div class="container">
        <div class="box" v-for="item in pizzaList" :key="item.id">
          <div>
            <img :src="item.imgUrl" alt="Image"/>
              <div>
                <a class="btn" @mouseenter="$event.currentTarget.style.background = '#EF6B7F'"
                @mouseleave="$event.currentTarget.style.background = '#e31837' ">Buy Now</a>
              </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data () {
    return {
      pizzaList: []
    }
  },
  mounted: function () {
    axios.get('http://localhost:8081/api/pizza/all')
    .then(response => {
      this.pizzaList = response.data;
    })
    .catch(e => {
      console.log(e);
    })
  }
}
</script>

I have already read Vue and API: Displaying Image Image path in Vue JS How to reference static assets within vue javascript我已经阅读了Vue 和 API:Displaying Image Image path in Vue JS How to reference static assets within vue javascript

but what these answers are telling is how to do when we have hardcoded the url, we should encapsulate it with require but they do not tell when we are getting the link from the DB, how do we put require in the html tag.但是这些答案告诉我们的是当我们对 url 进行硬编码时该怎么做,我们应该用require封装它但是他们没有告诉我们何时从数据库获取链接,我们如何将require放入 html 标签中。 I hope my question was clear.我希望我的问题很清楚。 Please ask for details if you need.如有需要请询问详情。 thanks谢谢

The reason it was not working was because它不起作用的原因是因为

Webpack's require() needs at least some part of the file path to be completely static and we should "make only part of the path dynamic"

that means you cannot put the entire image path in the DB and in the UI pass it to require and expect it to work这意味着您不能将整个图像路径放在数据库中,并在 UI 中将其传递给require并期望它工作

I replaced我换了

<img :src="require(`${item.imgUrl}`)" alt="Image"/>

where item.imgUrl = ' ../assets/entities/pizza/pizza_1.jpg ' (which is the entire image path relative to the component)其中 item.imgUrl = ' ../assets/entities/pizza/pizza_1.jpg '(这是相对于组件的整个图像路径)

by经过

<img :src="require(`../assets${item.imgUrl}`)" alt="Image"/>

where item.imgUrl = ' /entities/pizza/pizza_1.jpg '其中 item.imgUrl = ' /entities/pizza/pizza_1.jpg '

This answer mentioned by Michal Levy up in the comments explains all this https://stackoverflow.com/a/64208406/8147680 Michal Levy在评论中提到的这个答案解释了这一切https://stackoverflow.com/a/64208406/8147680

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM