tom-cnblogs

导航

vue3+vite+ts引入iconfont

一,iconfont建立项目库;
二,Symbol页复制js文件到assets
三,封装icon

点击查看代码
<template>
    <svg :class="svgClass" aria-hidden="true">
        <use :xlink:href="iconClassName" :fill="color" />
    </svg>
</template>
<script setup lang="ts">
import { computed } from 'vue';
const props = defineProps({
    iconName: {
        type: String,
        required: true
    },
    className: {
        type: String,
        default: ''
    },
    color: {
        type: String,
        default: '#5c6b77'
    }
});
// 图标在 iconfont 中的名字
const iconClassName = computed(() => {
    return `#${props.iconName}`;
})
// 给图标添加上类名
const svgClass = computed(() => {
    if (props.className) {
        return `svg-icon ${props.className}`;
    }
    return 'svg-icon';
});
</script>
<style scoped>
.svg-icon {
    width: 25px;
    height: 25px;
    position: relative;
    fill: currentColor;
    vertical-align: -2px;
}
</style>
四,main.ts引入
点击查看代码
import "@renderer/assets/icon"
import SvgIcon from "@renderer/components/icon/Index.vue"
const app = createApp(App)
app.component("SvgIcon", SvgIcon)

posted on 2025-08-31 14:18  tomcodes  阅读(97)  评论(0)    收藏  举报