Skip to content

iconfont图标symbol使用

图标地址:https://www.iconfont.cn/

使用方法

搜索需要的图标添加到自己的项目中,如果还没有项目,可以创建一个iconfont项目,然后将图标添加到该项目中。

组件代码

新建lau-icon.vue

vue
<template>
    <svg :class="svgClass" aria-hidden="true">
        <use :xlink:href="iconClassName" :fill="color" />
    </svg>
</template>
<script setup>
import {computed, ref} from 'vue';
const props = defineProps({
    iconName: {
        type: String,
        required: true
    },
    className: {
        type: String,
        default: ''
    },
    color: {
        type: String,
        default: ''
    },
    size: {
        type: Number,
        default: 16
    }
});
// 图标在 iconfont 中的名字
const iconClassName = computed(()=>{
    return `#${props.iconName}`;
})
const fontSize = ref(16)
// 给图标添加上类名
const svgClass = computed(() => {
    if (props.className) {
        return `svg-icon ${props.className}`;
    }

    fontSize.value = props.size
    return 'svg-icon';
});
</script>
<style scoped>
.svg-icon {
    width: 1em;
    height: 1em;
    position: relative;
    fill: currentColor;
    vertical-align: -2px;
    font-size: v-bind(fontSize + 'px');
}
</style>

vitePress使用

由于node是没有window的,所以在引入图标的时候需要在theme/index.js中添加如下:

js
   enhanceApp({app}) {
    app.mixin({
        async mounted() {
            //导入js插件window is not defined 处理
            //你自己的插件地址
            import('//at.alicdn.com/t/c/font_4191065_f2rp90fwead.js')
        },
    });
}

vue中使用

在main.js直接引入即可

使用示例

vue
<LauIcon icon-name="icon-jia1" :size="25"/>