vue项目引入自定义svg
图标可以使用element-ui的图标库、第三方的图标库或者引入svg使用,这里是讲如何使用自定义的svg。
- 将SVG图标放入项目
自定义的svg可以访问 https://www.iconfont.cn地址,搜索你想要的图标,下载 SVG 格式,放入项目的src/assets/icons/svg文件夹中。并在src/assets/icons/index.js中写上代码
1 2 3 4 5 6 7 | import Vue from 'vue' import SvgIcon from '@/components/SvgIcon' // svg component Vue.component( 'svg-icon' , SvgIcon) const req = require.context( './svg' , false , /\.svg$/) const requireAll = requireContext => requireContext.keys().map(requireContext) requireAll(req) |
2. 将SVG Sprite转换为Vue组件
你可以创建一个Vue组件来封装SVG Sprite,并在该组件中使用<symbol>
的id
来引用图标。这样,你就可以像使用其他Vue组件一样使用它。
在src/components/SvgIcon/index.vue中写入代码,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <template> <div v- if = "isExternal" :style= "styleExternalIcon" class = "svg-external-icon svg-icon" v-on= "$listeners" /> <svg v- else : class = "svgClass" aria-hidden= "true" v-on= "$listeners" > <use :xlink:href= "iconName" /> </svg> </template> <script> export default { name: 'SvgIcon' , props: { iconClass: { type: String, required: true }, className: { type: String, default : '' } }, computed: { isExternal() { return /^(https?:|mailto:|tel:)/.test( this .iconClass); }, iconName() { return ` #icon-${this.iconClass}`; }, svgClass() { if ( this .className) { return 'svg-icon ' + this .className; } else { return 'svg-icon' ; } }, styleExternalIcon() { return { mask: `url(${ this .iconClass}) no-repeat 50% 50%`, '-webkit-mask' : `url(${ this .iconClass}) no-repeat 50% 50%` }; } } }; </script> <style scoped> .svg-icon { width: 1em; height: 1em; vertical-align: -0.15em; fill: currentColor; overflow: hidden; } .svg-external-icon { background-color: currentColor; mask-size: cover!important; display: inline-block; } </style> |
然后,在你的主入口文件main.js
中全局注册这个组件,或者在需要的组件中局部注册。
1 | import '@/assets/icons' ; |
3. 使用Vue SVG Sprite loader
你可以使用svg-sprite-loader来自动将SVG文件转换为Vue组件。这样,你就可以像导入其他Vue组件一样导入SVG图标,并在模板中直接使用它们。
首先,安装svg-sprite-loader
1 | npm install svg-sprite-loader |
然后,在vue.config.js
中配置Webpack:
1 2 3 4 | const path = require( 'path' ) function resolve(dir) { return path.join(__dirname, dir) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | module.exports = { chainWebpack(config) { // set svg-sprite-loader 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: 'icon-[name]' }) .end() } } |
现在,你可以直接在你的Vue组件中导入SVG图标,并像使用其他组件一样使用它。
1 | <svg-icon icon- class = "svg图标名" class -name= '样式名' /> |
或者
1 2 3 | <el-button> <svg-icon icon- class = "svg图标名" class -name= '样式名' /> 新增 </el-button> |
完成上述操作之后,重启前端项目(我就是没重启,一直不显示出来)并刷新页面,这样就能看到图标了。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2019-03-07 Sqlserver查询所有数据库名、所有表名、所有表字段名