svg-sprite-vue-generator
Vue component runtime generator for svg-sprite-loader.
Install
npm install svg-sprite-vue-generator -D
Usage
Inline mode
All svg icon will be inline to part of the component.
Inline mode is default mode from svg-sprite-loader
. It called spriteModule
.
// FacebookSprite will be a valid vue component, use it as an vue component console// render as: <svg><use xlink:href="#facebook.sprite"></use></svg> console// render as: <svg><use xlink:href="#twitter.sprite"></use></svg> // wikipedia still be an normal asset url, use it as normal image asset url in html img tag or css background-image console// log as: /static/img/wikipedia.77b80eb8.svg
Config
May be need to install related loaders, such as:
- babel-loader
- svgo-loader
// vue.config.jsmoduleexports = { // default file-loader configmodule exclude // add svg-sprite-loader configmodule }
Extract mode
All svg file will be group to an independent svg file.
// FacebookSprite will be a valid vue component, use it as an vue component// It will be render as an normal img or svg tag with src to an an independent svg file(include all svg sprite) console// render as: <svg><use xlink:href="/static/sprite.svg#facebook.sprite-usage"></use></svg>// render as img: <img src="/static/sprite.svg#twitter.sprite--url-usage"> console// render as: <svg><use xlink:href="/static/sprite.svg#twitter.sprite-usage"></use></svg> // TwitterSpriteURL will be an normal url, use it as normal image asset in html img tag or css background-imageconsole// log as: /static/sprite.svg#twitter.sprite--url-usage // wikipedia still be an normal asset, use it as normal image asset in html img tag or css background-image console// log as: /static/img/wikipedia.77b80eb8.svg
Config
May be need to install related loaders, such as:
- svgo-loader
// vue.config.jsconst SpriteLoaderPlugin = moduleexports = assetsDir: 'static' configureWebpack: plugins: // NOTE: for extract mode, un support https://github.com/JetBrains/svg-sprite-loader#plain-sprite { // default file-loader configmodule exclude // add svg-sprite-loader configmodule }
TypeScript
add module declare for typescript.
declare module "*.sprite.svg" const content: VueConstructor<Vue>
Runtime Options
Use runtime options to config more
// default render svg tag, set it true to render img tag in extract mode extractCompTagImg: false // bind any attributes, default is empty attrs: {} // set loading class, default is 'svg-sprite-loading' loadingClass: '' // set any class string, default is '' otherClass: ''
Note
- Can not use inline and extract mode together
- May be you can use just extract mode for all svg file
Demo
npm run demo:serve