I'm trying to load images dynamically within a single file component, 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!