admin管理员组文章数量:1397065
I'm trying to load images dynamically within a single file ponent, but I'm getting an error that the module cannot be found. I think I'm trying to do the same thing as this SO post, but I'm not sure what I'm doing wrong. I used the webpack template to set up my project.
Here's the relevant markup in my template:
<router-link :to="{ name: 'JobsDetail', params: { slug: job.slug } }">
<span class="icon">
<img :src="getIconPath(job.slug)"/>
</span>
{{ job.title }}
</router-link>
And the function I'm trying to get the image from:
methods: {
getIconPath (iconName) {
const icons = require.context('../assets/', false, /\.png$/)
return iconName ? icons(`./${iconName}.png`) : ''
}
}
I have the images in the /src/assets/
directory. The error I'm getting in the browser console is:
[Vue warn]: Error in render: "Error: Cannot find module './some-icon.png'."
I'm not sure if this is a webpack config issue, or a problem with the getIconPath function. Anything help is appreciated, thanks!
I'm trying to load images dynamically within a single file ponent, but I'm getting an error that the module cannot be found. I think I'm trying to do the same thing as this SO post, but I'm not sure what I'm doing wrong. I used the webpack template to set up my project.
Here's the relevant markup in my template:
<router-link :to="{ name: 'JobsDetail', params: { slug: job.slug } }">
<span class="icon">
<img :src="getIconPath(job.slug)"/>
</span>
{{ job.title }}
</router-link>
And the function I'm trying to get the image from:
methods: {
getIconPath (iconName) {
const icons = require.context('../assets/', false, /\.png$/)
return iconName ? icons(`./${iconName}.png`) : ''
}
}
I have the images in the /src/assets/
directory. The error I'm getting in the browser console is:
[Vue warn]: Error in render: "Error: Cannot find module './some-icon.png'."
I'm not sure if this is a webpack config issue, or a problem with the getIconPath function. Anything help is appreciated, thanks!
Share Improve this question asked Mar 15, 2018 at 1:34 Charlie StanardCharlie Stanard 1,0682 gold badges10 silver badges21 bronze badges 2- Where is the file (path) that you have that JS code? – acdcjunior Commented Mar 15, 2018 at 1:59
-
@acdcjunior this is all inside of a single file ponent located in
/src/ponents/JobList.vue
– Charlie Stanard Commented Mar 15, 2018 at 2:01
2 Answers
Reset to default 4Considering your JavaScript code is at /src/ponents/JobList.vue
and your images are at /src/assets/
, use as follows:
methods: {
getIconPath (iconName) {
return iconName ? require(`../assets/${iconName}`) : ''
}
}
It looks like to me that the static assets such as images should be going to /static when webpack builds in dev or prod.
Looking in the config file: vuejs-templates/webpack/blob/develop/template/config/index.js, you have the following configuration (provided you haven't changed anything):
// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
So the src should probably be something like:
<img src="/static/whatEverMyIconIs.png"/>
EDIT:
Looking further into the configs, in vuejs-templates/webpack/blob/develop/template/build/webpack.base.conf.js, there's a url-loader plugin which should place images into /static/img:
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
Which could make your src url (depending on how the icons are being loaded):
<img src="/static/img/whatEverMyIconIs.png"/>
本文标签: javascriptVuejs dynamic image paths not loadingStack Overflow
版权声明:本文标题:javascript - Vue.js dynamic image paths not loading - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744148350a2592944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论