How can I get vue-loader to interpolate asset urls?

点点圈 提交于 2019-12-01 08:47:14
Pantelis Peslis

I'm pretty sure that your code throws a warning (not referred it):

[Vue warn]: src="./{{bar}}.jpg": interpolation in "src" attribute will cause a 404 request. Use v-bind:src instead.

So, you should bind the value:

<img :src="'/assets/' + bar + '.jpg'">

The above example it loads an image xxx.jpg from the static directory assets, but not via loader yet.

To accomplish that, you should use a dynamic require:

<img :src="loadImage(name)">

methods: {
  loadImage (imgName) {
    return require('./assets/' + imgName + '.jpg')
  }
}

NOTE

It is not recommended if the directory contains a large number of files, because the webpack will be load all the files which match your request (for the above example: ./assets/*.jpg).

In attribute interpolation is not allowed, use v-bind instead

https://vuejs.org/v2/guide/syntax.html#Attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!