在vue项目中快速使用svg图标解决方案
背景
在项目开发中大多数的icon图标都是前端给的svg或者在阿里巴巴图标库自己找的图标,然后img引入或者使用iconfont标签等等
但是不可避免的当图标更新或新增的时候比较麻烦,接下来借鉴下element的烧操作吧!
安装依赖
我们需要安装两个依赖,svg-sprite-loader和svgo
npm install svg-sprite-loader@6.0.11 --save-dev npm install svgo@3.0.0 --save-dev
这里面为了避免出现问题,我指定了安装版本,当前你可以自己尝试安装最新版~
编写处理svg文件的js
默认svg图片文件是带颜色的,要想实现svg图标颜色可以使用代码的color设置,那必须要处理下啦,我们可以手动一个一个删除svg中的fill属性代码(那太low了),下面是自动处理方式
创建目录【src/assets/icons/svg】后面这里面直接全部放svg图片即可
创建目录【src/assets/icons】这里面写自动处理js脚本
在目录icons中新建文件【index.js】
import Vue from 'vue' import SvgIcon from '@/components/SvgIcon'// svg component // register globally Vue.component('svg-icon', SvgIcon) const req = require.context('./svg', false, /\.svg$/) const requireAll = requireContext => requireContext.keys().map(requireContext) requireAll(req)
在目录icons中新建文件【svgo.config.js】
module.exports = { plugins: [ { name: "removeAttrs", params: { attrs: "(fill|fill-rule|fill-opacity)" } } ], };
在main.js中引入这个js
import './assets/icons' // icon
编写全局组件使其在vue中使用svg图标
创建目录【src/components/SvgIcon】
在SvgIcon目录下创建【index.vue】文件
<template> <svg :class="svgClass" aria-hidden="true" v-on="$listeners" :style="{ fontSize: size, color }"> <use :href="iconName" /> </svg> </template> <script> export default { name: 'SvgIcon', props: { iconClass: { type: String, required: true }, className: { type: String, default: '' }, size: { type: String }, color:{ type: String } }, computed: { iconName() { return `#myicon-${this.iconClass}` }, svgClass() { if (this.className) { return 'svg-icon ' + this.className } else { return 'svg-icon' } } } } </script> <style scoped> .svg-icon { width: 1em; height: 1em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; } </style>
修改vue配置文件vue.config.js
// test const path = require('path') function resolve(dir) { return path.join(__dirname, './', dir) }
然后再添加一个模块
chainWebpack: config => { config.module .rule('svg') .exclude.add(resolve('src/assets/icons')) .end(); config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/assets/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'myicon-[name]' }); }
完整示例如下:
// test const path = require('path') function resolve(dir) { return path.join(__dirname, './', dir) } module.exports = { publicPath: './', chainWebpack: config => { config.module .rule('svg') .exclude.add(resolve('src/assets/icons')) .end(); config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/assets/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'myicon-[name]' }); } }
修改package.json文件添加快捷指令
在【scripts】节点中添加如下代码:
"svgo": "svgo -f src/assets/icons/svg --config=src/assets/icons/svgo.config.js"
完整的【scripts】示例:
"scripts": { "serve": "node gen-router.js && vue-cli-service serve", "build": "node gen-router.js && vue-cli-service build", "genmenu": "node gen-router.js", "svgo": "svgo -f src/assets/icons/svg --config=src/assets/icons/svgo.config.js" },
使用组件
<svg-icon icon-class="home" size="22px" color="#ff5656"/>
这个home是放置在svg目录下的文件名称
完整示例:
<!--首页--> <template> <div> <svg-icon icon-class="home" size="22px" color="#ff5656"/> <svg-icon icon-class="home"/> </div> </template> <script> import svgIcon from "@/components/SvgIcon/index.vue"; export default { name: 'home', components: { svgIcon } }; </script> <style scoped> .home{ padding: 10px; } </style>
评论