• vue3 自动下载使用 iconify图标以及自定义svg图标


        开发中经常使用图标,通过unplugin-icons 插件我们可以在项目中自动下载所需的图标,但实际开发中我们还需要iconfont以及项目ui切图中的图标,这就需要使用自定义SvgIcon组件。具体操作步骤

    一、安装包

            npm i -D vite-plugin-svg-icons  unplugin-icons unocss

    二、自定义组件SvgIcon

    1. <script setup lang="ts" name="SvgIcon">
    2. import type {CSSProperties} from 'vue'
    3. const props = defineProps({
    4. name: {
    5. type: String,
    6. required: true,
    7. },
    8. color: {
    9. type: String,
    10. default: 'currentColor',
    11. },
    12. size: {
    13. type: [Number, String],
    14. default: 16,
    15. },
    16. spin: {
    17. type: Boolean,
    18. default: false,
    19. },
    20. })
    21. const symbolId = computed(() => `#${props.name}`)
    22. // 判断是否是自定义图标
    23. const isIcon = computed(() => props.name?.startsWith('icon'))
    24. const getStyle = computed((): CSSProperties => {
    25. const {size} = props
    26. let s = `${size}`
    27. s = `${s.replace('px', '')}px`
    28. return {fontSize: s,}
    29. })
    30. </script>
    31. <template>
    32. <i :class="['el-icon', spin && 'svg-icon-spin']" :style="getStyle">
    33. <svg v-if="isIcon" aria-hidden="true">
    34. <use :xlink:href="symbolId" rel="external nofollow" :fill="color"/>
    35. </svg>
    36. <span v-else :class="name"/>
    37. </i>
    38. </template>
    39. <style scoped lang="scss">
    40. .el-icon {
    41. svg {
    42. width: 1em;
    43. height: 1em;
    44. }
    45. }
    46. .svg-icon-spin {
    47. animation: circle 1.5s infinite linear;
    48. }
    49. /* 旋转动画 */
    50. @keyframes circle {
    51. 0% {
    52. transform: rotate(0);
    53. }
    54. 100% {
    55. transform: rotate(360deg);
    56. }
    57. }
    58. </style>

    三、配置

            vite.config.ts配置

    1. import vue from '@vitejs/plugin-vue';
    2. import {resolve} from 'path';
    3. import {defineConfig, loadEnv, ConfigEnv} from 'vite';
    4. // 这里获取element plus ant-design 中的图标数据对象
    5. // 在unocss 配置 就可以通过i-ep-图标名称 类名访问element plus 图标
    6. // 通过i-ant-design-图标名称访问 ant-design 图标
    7. // @iconify-json/ep @iconify-json/ant-design 自已安装或者在页面使用<i-ep-edit /> 图标 自动下载
    8. import {icons as ele} from '@iconify-json/ep'
    9. import {icons as ant} from '@iconify-json/ant-design'
    10. import Icons from 'unplugin-icons/vite'
    11. import IconsResolver from 'unplugin-icons/resolver'
    12. import {createSvgIconsPlugin} from "vite-plugin-svg-icons";
    13. import UnoCSS from 'unocss/vite'
    14. const pathResolve = (dir: string) => {
    15. return resolve(__dirname, '.', dir);
    16. };
    17. const viteConfig = defineConfig((mode: ConfigEnv) => {
    18. const env = loadEnv(mode.mode, process.cwd());
    19. return {
    20. plugins: [vue(),
    21. Icons({autoInstall: true, compiler: 'vue3'}),
    22. // 自定义图标配置
    23. createSvgIconsPlugin({
    24. // 指定要缓存的文件夹
    25. // eslint-disable-next-line no-undef
    26. iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
    27. // 指定symbolId格式
    28. symbolId: 'icon-[name]',
    29. //svgo额外配置,具体配置参考https://github.com/svg/svgo
    30. svgoOptions: {
    31. plugins: [
    32. {
    33. name: 'removeAttrs',
    34. params: {attrs: ['class', 'data-name', 'fill', 'stroke']}
    35. }
    36. ]
    37. }
    38. }),
    39. UnoCSS({
    40. safelist: [...Object.keys(ele.icons).map(name => `i-${ele.prefix}-${name}`),
    41. ...Object.keys(ant.icons).map(name => `i-${ant.prefix}-${name}`),
    42. ],// 引入
    43. // 以下配置是为了可以直接使用标签 <i-ep-edit />
    44. variants: [
    45. {
    46. match: (s) => {
    47. if (s.startsWith('i-')) {
    48. return {
    49. matcher: s,
    50. selector: (s) => {
    51. return s.startsWith('.') ? `${s.slice(1)},${s}` : s
    52. },
    53. }
    54. }
    55. },
    56. },
    57. ],
    58. }),
    59. root: process.cwd(),
    60. resolve: { '/@': pathResolve('./src/')},
    61. });
    62. export default viteConfig;

    unocss.config.ts配置

    1. import { defineConfig, presetAttributify, presetIcons, presetUno } from 'unocss'
    2. export default defineConfig({
    3. presets: [presetAttributify(), presetIcons(), presetUno()],
    4. })

    main.ts配置

    1. import {createApp} from 'vue';
    2. import App from './App.vue';
    3. import 'uno.css'
    4. import 'virtual:svg-icons-register'
    5. import SvgIcon from "/@/components/SvgIcon.vue";
    6. const app = createApp(App);
    7. // 注册全局
    8. app.component('SvgIcon', SvgIcon)
    9. app.mount('#app');

    四、使用

            自定义图标只要下载svg 文件到 assets/icons 文件夹下,自定义图标 通过icon-图标名称访问,iconify图标通过i-图标来源-图标名称访问 i-ep-edit   i-ant-design-user

  • 相关阅读:
    【API篇】五、Flink分流合流API
    素皮材质的手机壳,如何才能做到经久耐用?
    五、 通信协议
    一文2000字从0到1用Jmeter全流程性能测试实战
    LeetCode每日一题(803. Bricks Falling When Hit)
    一步步实现React-Hooks核心原理
    Win11蓝屏开不了机进入安全模式的快速方法
    LeetCode 1361. 验证二叉树【二叉树,DFS或BFS或并查集】1464
    VR全景应用广泛体现在哪里?有何优势?
    chrony 时间服务器 安装
  • 原文地址:https://blog.csdn.net/xw245184020/article/details/133759814