在配置化的情况下,图标配置也显得极为重要的
vite-plugin-svg-icons
动态配置参考vite-plugin-svg-icons
npm i vite-plugin-svg-icons -D
import { resolve } from 'path'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
export default defineConfig(({ command, mode }) => {
...
return {
plugins: [
...
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
// iconDirs: [resolve(pathSrc, "assets/icons")],
// iconDirs: [resolve(__dirname, "src/assets/icons")],
iconDirs: [resolve(process.cwd(), 'src/assets/icons')], // 指定项目内图标路径
// 指定symbolId格式
symbolId: "icon-[dir]-[name]",
}),
]
}
import 'virtual:svg-icons-register'
下载方式例举几个
把需要使用的svg图标放置这个src/assets/icons
目录
svg-icon.vue
<template>
<svg
aria-hidden="false"
class="svg-icon"
:style="'width:' + size + ';height:' + size"
>
<use :xlink:href="symbolId" :fill="color" />
svg>
template>
<script setup lang="ts">
import { computed } from "vue";
const props = defineProps({
prefix: {
type: String,
default: "icon",
},
iconClass: {
type: String,
required: false,
default: "",
},
color: {
type: String,
default: "",
},
size: {
type: String,
default: "20px",
},
});
setTimeout(() => {
console.log(symbolId)
}, 5000);
const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`);
script>
<style scoped>
.svg-icon {
display: inline-block;
width: 1em;
height: 1em;
overflow: hidden;
vertical-align: -0.15em; /* 因icon大小被设置为和字体大小一致,而span等标签的下边缘会和字体的基线对齐,故需设置一个往下的偏移比例,来纠正视觉上的未对齐效果 */
outline: none;
fill: currentcolor; /* 定义元素的颜色,currentColor是一个变量,这个变量的值就表示当前元素的color值,如果当前元素未设置color值,则从父元素继承 */
}
style>
<SvgIcon v-if="menu.icon" :icon-class="icon" />
import { defineComponent, ref } from "vue";
import SvgIcon from "@/components/svg-icon/index.vue";
export default defineComponent({
components: {
SvgIcon,
},
setup() {
const icon = ref('document')
return {
icon,
};
},
});
可以引入Element-Plus提供的icon组件,也可自定义icon组件
<template>
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
fill="currentColor"
>
<path
d="M15 4a1 1 0 1 0 0 2V4zm0 11v-1a1 1 0 0 0-1 1h1zm0 4l-.707.707A1 1 0 0 0 16 19h-1zm-4-4l.707-.707A1 1 0 0 0 11 14v1zm-4.707-1.293a1 1 0 0 0-1.414 1.414l1.414-1.414zm-.707.707l-.707-.707.707.707zM9 11v-1a1 1 0 0 0-.707.293L9 11zm-4 0h1a1 1 0 0 0-1-1v1zm0 4H4a1 1 0 0 0 1.707.707L5 15zm10-9h2V4h-2v2zm2 0a1 1 0 0 1 1 1h2a3 3 0 0 0-3-3v2zm1 1v6h2V7h-2zm0 6a1 1 0 0 1-1 1v2a3 3 0 0 0 3-3h-2zm-1 1h-2v2h2v-2zm-3 1v4h2v-4h-2zm1.707 3.293l-4-4-1.414 1.414 4 4 1.414-1.414zM11 14H7v2h4v-2zm-4 0c-.276 0-.525-.111-.707-.293l-1.414 1.414C5.42 15.663 6.172 16 7 16v-2zm-.707 1.121l3.414-3.414-1.414-1.414-3.414 3.414 1.414 1.414zM9 12h4v-2H9v2zm4 0a3 3 0 0 0 3-3h-2a1 1 0 0 1-1 1v2zm3-3V3h-2v6h2zm0-6a3 3 0 0 0-3-3v2a1 1 0 0 1 1 1h2zm-3-3H3v2h10V0zM3 0a3 3 0 0 0-3 3h2a1 1 0 0 1 1-1V0zM0 3v6h2V3H0zm0 6a3 3 0 0 0 3 3v-2a1 1 0 0 1-1-1H0zm3 3h2v-2H3v2zm1-1v4h2v-4H4zm1.707 4.707l.586-.586-1.414-1.414-.586.586 1.414 1.414z"
/>
svg>
template>
<script lang="ts" src="./index.ts" />
<template>
<div v-for="(icon, index) in ElIcons" :key="icon + index">
<el-icon><component :is="icon">component>el-icon>
div>
template>
import { defineComponent, reactive } from "vue";
import IconCommunity from "@/components/icons/IconCommunity.vue";
import Menu from "@/components/menu/index.vue";
import {
Document,
Menu as IconMenu,
Location,
Setting,
} from '@element-plus/icons-vue'
export default defineComponent({
components: {
Document,
IconMenu,
Location,
Setting,
IconCommunity
},
setup() {
const ElIcons = reactive(['Document', 'IconMenu', 'Location', 'Setting', 'IconCommunity'])
return {
ElIcons,
};
},
});
如有启发,可点赞收藏哟~