I'd like to import a directory full of SVG assets, and reference those as an array within my VueJS component. Something like the following:
//LevelComponent.vue
<template>
<div v-html="image"></div>
</template>
<script>
import * as levels from '@/assets/levels/*.svg'
export default {
name: "Level",
data() {
return {
image: ''
}
},
methods: {
setLevel(levelNumber) {
this.level = levels[levelNumber];
}
}
}
</script>
The above obviously doesn't work because of how VueJS/Webpack handles asset paths:
Module not found: Error: Can't resolve '@/assets/levels/*.svg'
We currently load the SVGs separately, but this requires a update in our code every time the contents of the directory changes:
import level1 from '@/assets/levels/1.svg'
import level2 from '@/assets/levels/2.svg'
import level3 from '@/assets/levels/3.svg'
Other than the above error, I'm pretty sure I'm going about this the wrong way. What would the correct way be to approach this? I should mention we currently use svg-inline-loader
in Webpack to load the individual SVGs.