下载icon图标(svg格式)
用到的是腾讯codesign,其他平台也能进行svg图片的下载

放入项目

svg中如何控制图标颜色
如下图控制svg颜色的是fill参数,此处可以删除掉,保证添加后和同级文字样式统一

引入插件
在vite.config.ts中引入插件vite-plugin-svg-icons
官方文档:https://github.com/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>
本文介绍如何在Vite项目中加载和使用SVG图标,并通过自定义组件实现图标颜色的动态调整。文中详细解释了配置vite-plugin-svg-icons插件的方法,以及如何创建SVG图标组件来方便地调用项目中的SVG文件。
792

被折叠的 条评论
为什么被折叠?



