admin管理员组文章数量:1321808
The issue I'm having is using vue.js with webpack.
I have a list that I want to dynamically populate in vue.js.
I am using v-for to iterate over each object and create the list items.
To bind a img src tag to data point {{resource.img}} it looks like this: <img v-bind:src="resource.img">
to dynamically load an image.
However, when the client renders the page, the src path is correct, however it is the actual relative path (e.g. "../assets/example.png" etc, instead of the path webpack outputs. Thus, the image creates a 404 because there is no image at "../assets/example.png".
Alternatively, if I hardcode the src the image loads, and the the src is some path that webpack created, finding the correct image.
How can I dynamically set the src with webpack and vue.js?
The issue I'm having is using vue.js with webpack.
I have a list that I want to dynamically populate in vue.js.
I am using v-for to iterate over each object and create the list items.
To bind a img src tag to data point {{resource.img}} it looks like this: <img v-bind:src="resource.img">
to dynamically load an image.
However, when the client renders the page, the src path is correct, however it is the actual relative path (e.g. "../assets/example.png" etc, instead of the path webpack outputs. Thus, the image creates a 404 because there is no image at "../assets/example.png".
Alternatively, if I hardcode the src the image loads, and the the src is some path that webpack created, finding the correct image.
How can I dynamically set the src with webpack and vue.js?
Share Improve this question asked Oct 6, 2016 at 22:50 cchingcching 7991 gold badge8 silver badges17 bronze badges1 Answer
Reset to default 10You need require.context.
For example:
<img width="30" height="30" :src="imgUrl(resource.img)">
var images = require.context('../../assets/img/', false, /\.jpg$/)
export default {
methods: {
imgUrl: function (path) {
return images('./' + path)
}
}
}
The require.context will essentially map the source path to the final path in the build directory for any image that it found in the source folder. This is how the imgUrl() method will return the right path at runtime.
本文标签: javascriptCan39t dynamically pass relative src path for imgs in VuejswebpackStack Overflow
版权声明:本文标题:javascript - Can't dynamically pass relative src path for imgs in Vue.js + webpack - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742106523a2421042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论