Hi, I 'm new to vue.js, nuxt.js, webpack tooling. My project is a nuxt-create-app.
First I have to say it is amazing toolset that I was looking for a long time. So exiting to do new projects with it.
My problem is I have an image that I want to check if it exist first and if not, require the default image.
I made a function in Vue filters with a JS try catch, but a got the error: Cannot find module '@/assets/img/beer-default.png'.
I try different approach to reference the path like ./assets… or .assets… but It didn’t work neither.
I think node require() is working because of the error message I receive. The path reference is probably wrong and maybe Webpack can not resolve the path (@ or .) in the function.
I also want that you know that :src="require('@/assets/img/beer-' + productId + '.png')"
is working.
If someone can help me to find the right way to refers the relative path in a Vue filters function or if I’m doing something wrong or impossible, I would really appreciate.
Here’s the code
<img
:src="'@/assets/img/beer-' + productId + '.png' | loadImgOrPlaceholder('@/assets/img/beer-default.png')"
:alt="$t('brand_name') + ' ' + product.name"
class="product-img is-block"
>
<script>
export default {
filters: {
loadImgOrPlaceholder: function (path, fallback) {
let img = ""
try {
img = require(path)
return img
} catch (e) {
img = require(fallback)
return img
}
}
}
}
</script>
Thank you for your attention