简体   繁体   English

如何动态绑定vue.js中img的src?

[英]How to dynamically bind to :src of img in vue js?

Following is the code for binding to src attribute of img tag.以下是绑定到 img 标签的 src 属性的代码。 I'm being able to bind to the attribute.我能够绑定到属性。 But the link is not getting the real icon from assets folder, instead shows a 404 or not found.但是链接没有从资产文件夹中获取真正的图标,而是显示 404 或未找到。

  <router-link
    v-for="nav in navigations"
    :to="'/' + nav"
  >
    <img
      class="icon"
      :src="'@/assets/icons/'+nav+'.svg'"
    />
    {{navigation}}
  </router-link>

Real question: How can I dynamically bind to an attribute with a variable between string ie '@/assets/icons/' + nav + '.svg' ?真正的问题:我如何动态绑定到一个带有字符串之间变量的属性,即'@/assets/icons/' + nav + '.svg' like this?像这样?

When I inspect the element it is showing the same path.当我检查元素时,它显示相同的路径。 ie @/assets/icons/{{iconnamehere}}.svg But the real icon is not showing up.@/assets/icons/{{iconnamehere}}.svg但真正的图标并没有出现。

But if I do something like this without any binding <img src="@/assets/icons/logo.svg" />但是如果我在没有任何绑定的情况下做这样的事情<img src="@/assets/icons/logo.svg" />

then the image shows... When I inspect the element I see a replaced path something like this `然后图像显示...当我检查元素时,我看到一个替换路径,类似于这样的`

Hope you have figured this out by now, but turns out you have to do the following:希望你现在已经弄明白了,但事实证明你必须执行以下操作:

  methods: {
    getPic (name) {
      return require("@/assets/icons/"+name+".svg")
    }
  }
    <img
      class="icon"
      :src="getPic(nav)"
    />

Following is the code for binding to src attribute of img tag.下面是绑定到img标签的src属性的代码。 I'm being able to bind to the attribute.我能够绑定到该属性。 But the link is not getting the real icon from assets folder, instead shows a 404 or not found.但是链接没有从资产文件夹中获取真正的图标,而是显示 404 或未找到。

  <router-link
    v-for="nav in navigations"
    :to="'/' + nav"
  >
    <img
      class="icon"
      :src="'@/assets/icons/'+nav+'.svg'"
    />
    {{navigation}}
  </router-link>

Real question : How can I dynamically bind to an attribute with a variable between string ie '@/assets/icons/' + nav + '.svg' ?真正的问题:如何使用字符串之间的变量动态绑定到属性,即'@/assets/icons/' + nav + '.svg' like this?像这样?

When I inspect the element it is showing the same path.当我检查元素时,它显示相同的路径。 ie @/assets/icons/{{iconnamehere}}.svg But the real icon is not showing up.@/assets/icons/{{iconnamehere}}.svg但真正的图标并没有显示出来。

But if I do something like this without any binding <img src="@/assets/icons/logo.svg" />但是如果我在没有任何绑定的情况下做这样的事情<img src="@/assets/icons/logo.svg" />

then the image shows... When I inspect the element I see a replaced path something like this `然后图像显示......当我检查元素时,我看到一个替换的路径是这样的`

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

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