图标选择器与图标使用指南
项目当前使用 @iconify/vue 作为主要图标库,配合 UnoCSS 的 preset-icons 实现按需加载。
图标集预生成
在项目中,由于图标选择器的特殊性,需要预先指定并生成相应的文件以打包大量图标。以下是生成图标集的详细步骤:
图标集预生成
在项目中,由于图标选择器的特殊性,需要预先指定并生成相应的文件以打包大量图标。以下是生成图标集的详细步骤:
图标集生成步骤
-
执行图标生成命令:
yarn gen:icon -
选择生成方式:
- 选择“local”表示本地生成,选择“online”表示在线生成。每种方式都有其优缺点,具体如下:
- 本地生成:将所有图标打包进 JavaScript 文件中,适用于无法访问外网的环境。
- 在线生成:在图标选择器使用图标时在线请求,并缓存到浏览器,减少代码打包体积。
- 选择“local”表示本地生成,选择“online”表示在线生成。每种方式都有其优缺点,具体如下:

- 选择图标集:
- 根据提示选择要生成的图标集,并按回车确认。

- 选择输出目录:
- 选择图标输出的目录,默认为
src/components/Icon/data,可以直接回车选择默认路径。
- 选择图标输出的目录,默认为

完成以上步骤后,图标集生成完成,图标选择器将包含所选图标集中的图标。注意,频繁更新图标集可能会导致图标丢失或显示不出来,特别是在本地生成的情况下。
生成方式的优缺点
-
在线图标(项目默认推荐):
- 优点:有效减少代码打包体积,适用于可以访问外网的项目。
- 缺点:在无法访问外网的环境中,图标可能无法显示。
-
本地图标:
- 优点:不依赖外网,适用于所有环境。
- 缺点:打包体积较大,具体体积取决于所选图标集的图标数量。
组件库图标使用
使用 ant-design-vue 提供的图标
<template>
<StarOutlined />
<StarFilled />
<StarTwoTone twoToneColor="#eb2f96" />
</template>
<script>
import { defineComponent } from 'vue';
import { StarOutlined, StarFilled, StarTwoTone } from '@ant-design/icons-vue';
export default defineComponent({
components: { StarOutlined, StarFilled, StarTwoTone },
});
</script>Svg Sprite 图标
使用方法
-
将 SVG 图标放置于
src/assets/icons目录下,例如:test.svg。 -
使用
SvgIcon组件展示图标:
<template>
<SvgIcon name="test" />
</template>
<script>
import { defineComponent } from 'vue';
import { SvgIcon } from '/@/components/Icon';
export default defineComponent({
components: { SvgIcon },
});
</script>- 使用
Icon组件展示图标,以|svg结尾的图标名将自动使用SvgIcon组件:
<template>
<Icon name="test|svg" />
</template>
<script>
import { defineComponent } from 'vue';
import { Icon } from '/@/components/Icon';
export default defineComponent({
components: { Icon },
});
</script>Iconify 图标
项目中使用 vite-plugin-purge-icons 插件实现 Iconify 图标。
- 安装依赖:
yarn add @iconify/iconify
yarn add @iconify/json @purge-icons/generated -D- 在
vite.config.ts中引入插件:
import PurgeIcons from 'vite-plugin-purge-icons';
export default {
plugins: [PurgeIcons()],
};- 编写 Icon 组件,完整代码见 src/components/Icon/src/Icon.vue。
<template>
<SvgIcon :size="size" :name="getSvgIcon" v-if="isSvgIcon" :class="[$attrs.class]" :spin="spin" />
<span
v-else
ref="elRef"
:class="[$attrs.class, 'app-iconify anticon', spin && 'app-iconify-spin']"
:style="getWrapStyle"
></span>
</template>
<script lang="ts">
import type { PropType } from 'vue';
import {
defineComponent,
ref,
watch,
onMounted,
nextTick,
unref,
computed,
CSSProperties,
} from 'vue';
import SvgIcon from './SvgIcon.vue';
import Iconify from '@purge-icons/generated';
import { isString } from '/@/utils/is';
import { propTypes } from '/@/utils/propTypes';
const SVG_END_WITH_FLAG = '|svg';
export default defineComponent({
name: 'GIcon',
components: { SvgIcon },
props: {
icon: propTypes.string,
color: propTypes.string,
size: {
type: [String, Number] as PropType<string | number>,
default: 16,
},
spin: propTypes.bool.def(false),
prefix: propTypes.string.def(''),
},
setup(props) {
const elRef = ref<ElRef>(null);
const isSvgIcon = computed(() => props.icon?.endsWith(SVG_END_WITH_FLAG));
const getSvgIcon = computed(() => props.icon.replace(SVG_END_WITH_FLAG, ''));
const getIconRef = computed(() => `${props.prefix ? props.prefix + ':' : ''}${props.icon}`);
const update = async () => {
if (unref(isSvgIcon)) return;
const el = unref(elRef);
if (!el) return;
await nextTick();
const icon = unref(getIconRef);
if (!icon) return;
const svg = Iconify.renderSVG(icon, {});
if (svg) {
el.textContent = '';
el.appendChild(svg);
} else {
const span = document.createElement('span');
span.className = 'iconify';
span.dataset.icon = icon;
el.textContent = '';
el.appendChild(span);
}
};
const getWrapStyle = computed((): CSSProperties => {
const { size, color } = props;
let fs = size;
if (isString(size)) {
fs = parseInt(size, 10);
}
return {
fontSize: `${fs}px`,
color: color,
display: 'inline-flex',
};
});
watch(() => props.icon, update, { flush: 'post' });
onMounted(update);
return { elRef, getWrapStyle, isSvgIcon, getSvgIcon };
},
});
</script>
<style lang="less">
.app-iconify {
display: inline-block;
&-spin {
svg {
animation: loadingCircle 1s infinite linear;
}
}
}
span.iconify {
display: block;
min-width: 1em;
min-height: 1em;
background-color: @iconify-bg-color;
border-radius: 100%;
}
</style>页面信息›
已优化是