admin管理员组文章数量:1279014
I've just started using Vue.js, it's simple enough accessing the assets folder when loading a static img such as my logo:
<img src="../assets/logo.png">
however, I'm using v-for to create several list items using sample data, this is my template:
<ul>
<li v-for="item in items" :key="item.id">
<img v-bind:src="item.img" v-bind:alt="item.title">
<h2 class="md-title">{{item.title}}</h2>
</li>
</ul>
and this is the sample data declared within the same .vue file:
export default {
name: 'front',
data () {
return {
items: [
{title: 'Google Pixel', img: '../assets/pixel.png', id: 1},
{title: 'Samsung Galaxy S8', img: '../assets/s8.png', id: 2},
{title: 'iPhone 7', img: '../assets/iphone.png', id: 3}
]
}
}
}
My problem is that the imgs all 404 which seems weird since the same path works on static imgs. If I use an absolute path such as this it works
?$xl-standard$
Having looked through the documentation I found this .html
Which might explain it but I've tried each method suggested without any luck:
Asset Resolving Rules
Relative URLs, e.g. ./assets/logo.png will be interpreted as a module dependency. They will be replaced with an auto-generated URL based on your Webpack output configuration.
Non-prefixed URLs, e.g. assets/logo.png will be treated the same as the relative URLs and translated into ./assets/logo.png.
URLs prefixed with ~ are treated as a module request, similar to require('some-module/image.png'). You need to use this prefix if you want to leverage Webpack's module resolving configurations. For example if you have a resolve alias for assets, you need to use to ensure that alias is respected.
Root-relative URLs, e.g. /assets/logo.png are not processed at all.
is this an issue with webpack instead of vue? it seems crazy that a library which is so easy to use would make something like filepaths plex.
I've just started using Vue.js, it's simple enough accessing the assets folder when loading a static img such as my logo:
<img src="../assets/logo.png">
however, I'm using v-for to create several list items using sample data, this is my template:
<ul>
<li v-for="item in items" :key="item.id">
<img v-bind:src="item.img" v-bind:alt="item.title">
<h2 class="md-title">{{item.title}}</h2>
</li>
</ul>
and this is the sample data declared within the same .vue file:
export default {
name: 'front',
data () {
return {
items: [
{title: 'Google Pixel', img: '../assets/pixel.png', id: 1},
{title: 'Samsung Galaxy S8', img: '../assets/s8.png', id: 2},
{title: 'iPhone 7', img: '../assets/iphone.png', id: 3}
]
}
}
}
My problem is that the imgs all 404 which seems weird since the same path works on static imgs. If I use an absolute path such as this it works
https://media.carphonewarehouse./is/image/cpw2/pixelBLACK?$xl-standard$
Having looked through the documentation I found this https://vuejs-templates.github.io/webpack/static.html
Which might explain it but I've tried each method suggested without any luck:
Asset Resolving Rules
Relative URLs, e.g. ./assets/logo.png will be interpreted as a module dependency. They will be replaced with an auto-generated URL based on your Webpack output configuration.
Non-prefixed URLs, e.g. assets/logo.png will be treated the same as the relative URLs and translated into ./assets/logo.png.
URLs prefixed with ~ are treated as a module request, similar to require('some-module/image.png'). You need to use this prefix if you want to leverage Webpack's module resolving configurations. For example if you have a resolve alias for assets, you need to use to ensure that alias is respected.
Root-relative URLs, e.g. /assets/logo.png are not processed at all.
is this an issue with webpack instead of vue? it seems crazy that a library which is so easy to use would make something like filepaths plex.
Share Improve this question edited Aug 13, 2020 at 13:40 Pixelomo asked Apr 17, 2017 at 8:17 PixelomoPixelomo 6,7374 gold badges48 silver badges62 bronze badges1 Answer
Reset to default 12I posted this question on Vue.js' forum and Linus Borg got back to me with the answer:
Those explanations only refer to asset paths used in HTML or CSS. in these, the respective webpack-loaders can recognize those paths and manage them.
But if you have paths in your Javascript, webpack cannot infer that those strings should be treated as paths (it can't "understand" your program and see that those strings will later be used in HTML), so you have to tell webpack explicitly that those strings are indeed paths to dependencies, by using require().
export default {
name: 'front',
data () {
return {
items: [
{title: 'Google Pixel', img: require('../assets/pixel.png'), id: 1},
{title: 'Samsung Galaxy S8', img: require('../assets/s8.png'), id: 2},
{title: 'iPhone 7', img: require('../assets/iphone.png'), id: 3}
]
}
}
}
本文标签: javascriptVuejs assets file path when using vforStack Overflow
版权声明:本文标题:javascript - Vue.js assets file path when using v-for - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741273397a2369590.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论