Last active
February 18, 2022 13:03
-
-
Save simonpioli/5c27c27e7d9d826ee9b722de1eedc838 to your computer and use it in GitHub Desktop.
Vue mixin to Use Laravel Mix manifest in templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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