Skip to content

Instantly share code, notes, and snippets.

@simonpioli
Last active February 18, 2022 13:03
Show Gist options
  • Save simonpioli/5c27c27e7d9d826ee9b722de1eedc838 to your computer and use it in GitHub Desktop.
Save simonpioli/5c27c27e7d9d826ee9b722de1eedc838 to your computer and use it in GitHub Desktop.
Vue mixin to Use Laravel Mix manifest in templates
<template>
<figure><img :src="mix('example-asset.jpg')" alt=""></figure>
</template>
<script>
import {mix} from "./mix";
export default {
name: "ExampleComponent",
mixins: [mix]
}
</script>
<style scoped>
</style>
export const mix = {
data: () => ({
manifest: {}
}),
async created() {
const self = this;
await fetch('/mix-manifest.json')
.then(response => response.json())
.then(manifest => {
self.manifest = manifest;
});
},
methods: {
mix(assetUrl) {
if (this.manifest) {
return this.manifest[assetUrl];
}
console.warn('Laravel Mix (Vue): No manifest set.');
return assetUrl;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment