webpack实现
下载icon图标(svg格式)
用到的是腾讯codesign,其他平台也能进行svg图片的下载
放入项目
svg中如何控制图标颜色
如下图控制svg颜色的是fill参数,此处可以删除掉,保证添加后和同级文字样式统一
引入插件
在vite.config.ts
中引入插件vite-plugin-svg-icons
官方文档:/vbenjs/vite-plugin-svg-icons/tree/main#readme
在plugins
中使用
此处需要在plugins中,匹配到svg的目录
引入svg组件
svg组件
<template><svg aria-hidden="true" :class="`iconfont ${className}`" :style="`width:${width};height:${height};color:${color};${style}`"><use :xlink:href="symbolId" /></svg></template><script lang="ts">import {computed, defineComponent } from "vue";/*** 自定义svg图标,可自行将svg图标下载后存放在/src/assets/icons/svg目录下* `使用方法:<svg-icon name="earth" color="red"></svg-icon>`*/export default defineComponent({name: "SvgIcon",props: {prefix: {type: String,default: "icon"},name: {type: String,required: true},color: {type: String,default: ""},width: String,height: String,className: {type: String, default: "" },style: {type: String, default: "" }},setup(props) {const symbolId = computed(() => `#${props.prefix}-${props.name.replace("icon-", "")}`);return {symbolId };}});</script>
svg组件使用
/*** 自定义svg图标,可自行将svg图标下载后存放在/src/assets/icons/svg目录下* `使用方法:<svg-icon name="earth" color="red"></svg-icon>`*/
<template><div class="hidden-xs-only"><svg-icon :name="state.collapseSidebar ? 'indent' : 'outdent'" /></div><div class="hidden-sm-and-up show-xs-only"><svg-icon name="icon-indent" /></div></template><script>import SvgIcon from '@/components/base/svg-icon'export default defineComponent({components: {SvgIcon},setup(){})}</script>