- [Vuejs]-Change the CSS if dynamically changing image is loaded in Vue 3
- [Vuejs]-Vue js Tailwind card design with image out of the card
1👍
The error is because you are defining a string into a vue binding
:src=""
This expects a javascript constant
You could just do
src="/images/test.png"
But this will cause errors as you navigate to sub routes and pages.
In Laravel there is an asset helper and it looks like this
asset('images/test.png')
This will output a full url
http://localhost:8000/images/test.png
This asset helper is useful in PHP. But is not available to JS
So you can make it available using a vue mixin
In the head section of your HTML
Add this
<script> window._asset = '{{ asset('') }}'; </script>
This will bind the asset helper to the window
Next create a mixin in your app/js
It will look something like this
module.exports = {
methods: {
asset(path) {
var base_path = window._asset || '';
return base_path + path;
}
}
}
The final step is to require the mixin
in your app.js
Vue.mixin(require('./asset'));
Then you can use it in vue
<img :src="asset('images/test.png')" />
Source:stackexchange.com