Understand how SVG-Icon works
How can you apply it to a new project?
- Svg-icon is a global component in
src/icons/index.js
Global components are defined and registered in. - Introduced in main.js
- It takes extra
[email protected]
And the corresponding configuration in vue.config.js. - Its use format
< svG-icon icon-class=" filename "/>
. The file name in this case is the file name under SRC/ICONS/SVG
How to use the SVG-icon component
1. The installation depends on NPM I [email protected]
2. Configure the vue. Config. Js
const path = require('path')
function resolve (dir) {
return path.join(__dirname, dir)
}
Copy the code
Add a configuration {}
{
// omit other...
chainWebpack (config) {
// set svg-sprite-loader
config.module
.rule('svg')
.exclude.add(resolve('src/icons'))
.end()
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end()
}
}
Copy the code
3. Copy or create a file
(1) src/icons
src/icons
- /svg // Save icon - / Index.js // Register the global component
Copy the code
(2) components
componets/SvgIcon/index.vue
(3) validate. Js
utils/validate.js
4. Import in main.js
import '@/icons'
5. Use
Use the format < svG-icon icon-class=” file name “/>. The file name in this case is the file name under SRC/ICONS/SVG