npm i raw-loader @nuxtjs/svg-sprite
Last active
August 20, 2021 18:18
-
-
Save internetztube/42a1410aa9f3d042d0521e8d69480cc9 to your computer and use it in GitHub Desktop.
Nuxt Inline SVG Symbols
This file contains hidden or 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> | |
<svg> | |
<use :href="`#i-${handle}`"></use> | |
</svg> | |
</template> | |
<script> | |
export default { | |
props: { | |
handle: { | |
type: String, | |
required: true | |
} | |
} | |
} | |
</script> |
This file contains hidden or 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> | |
<div> | |
<svg-symbols></svg-symbols> | |
<nuxt /> | |
</div> | |
</template> |
This file contains hidden or 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
module.exports = { | |
modules: [ | |
'@nuxtjs/svg-sprite', | |
] | |
} |
This file contains hidden or 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
import Vue from 'vue' | |
// eslint-disable-next-line import/no-webpack-loader-syntax | |
const svgFileContent = require('!!raw-loader!../assets/sprite/gen/icons.svg').default | |
Vue.component('svg-symbols', { | |
render: function (createElement) { | |
const innerHTML = svg.replace(/.*?$/m, '') // strip declaration | |
const style = 'height: 0; width: 0; position: absolute; top: 0; z-index: -1' | |
return createElement('div', { domProps: { innerHTML }, attrs: { style } }) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, @leviwheatcroft! Updated the Snippet.