Vue3+TS中svg图标的利用-@unocss/preset-icons

打印 上一主题 下一主题

主题 1780|帖子 1780|积分 5340

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
@unocss/preset-icons 是 UnoCSS 提供的图标预设,支持从 本地和在线图标库 加载图标,本文介绍本地图标库的利用
  从 https://blog.csdn.net/u013737132/article/details/145499595 得知vite-plugin-svg-icons 插件停止维护,依赖过时,便整理替代品
预备svg文件

   src/assets/icons下随意创建文件夹custom,menu

从网络上下载svg文件复制到两个文件夹下
  安装依赖

  1. pnpm add -D unocss @iconify/utils glob
复制代码
配置文件

vite.config.ts
  1. ...
  2. import vue from '@vitejs/plugin-vue'
  3. // 导入Unocss插件
  4. import Unocss from 'unocss/vite'
  5. ...
  6. export default defineConfig({
  7.         plugins: [
  8.         vue(),
  9.         // 注册Unocss插件
  10.         Unocss(),
  11.     ]
  12. })
复制代码
uno.config.ts
  1. import { defineConfig, presetIcons, presetWind3 } from "unocss";
  2. import { FileSystemIconLoader } from "@iconify/utils/lib/loader/node-loaders";
  3. import { globSync } from 'glob'
  4. import path from 'node:path'
  5. /**
  6. * 获取 src/assets/icons/ 目录下的所有 svg图标文件
  7. * 实际只适用 icons的下一级目录,再下级目录不支持
  8. */
  9. const iconsDirPattern = "./src/assets/icons/**/*.svg";
  10. const files = globSync(iconsDirPattern, { nodir: true }) // nodir:true 不匹配目录,只匹配文件
  11. // 读取本地 SVG 目录,获取所有 svg 图标,并按照文件夹分类
  12. /**
  13. * 返回数据格式
  14. * {
  15. *   menu: [ 'i-menu:home' ],
  16. *   custom: [
  17. *     'i-custom:welcome',
  18. *     'i-custom:refresh',
  19. *     'i-custom:phone',
  20. *     'i-custom:logout',
  21. *     'i-custom:lock',
  22. *     'i-custom:loading',
  23. *     'i-custom:juejin',
  24. *     'i-custom:home',
  25. *     'i-custom:full-screen',
  26. *     'i-custom:exit-full',
  27. *     'i-custom:copyright'
  28. *   ]
  29. * }
  30. */
  31. function getIcons() {
  32.   const icons = {}
  33.   files.forEach((filePath) => {
  34.     const fileName = path.basename(filePath) // 获取文件名,包括后缀
  35.     const fileNameWithoutExt = path.parse(fileName).name // 获取去除后缀的文件名
  36.     const folderName = path.basename(path.dirname(filePath)) // 获取文件夹名
  37.     if (!icons[folderName]) {
  38.       icons[folderName] = []
  39.     }
  40.     icons[folderName].push(`i-${folderName}:${fileNameWithoutExt}`)
  41.   })
  42.   return icons
  43. }
  44. const icons = getIcons()
  45. /**
  46. * 读取本地 SVG 目录,获取所有 svg 图标,并按照文件夹分类
  47. * 返回数据格式
  48. * collections {
  49. *   menu: [AsyncFunction (anonymous)],
  50. *   custom: [AsyncFunction (anonymous)]
  51. * }
  52.   */
  53. const collections = Object.fromEntries(Object.keys(icons).map(item => [
  54.   item,
  55.   FileSystemIconLoader(`src/assets/icons/${item}`, (svg) => {
  56.     return svg.includes('fill="')
  57.       ? svg
  58.       : svg.replace(/^<svg /, '<svg fill="currentColor" ');
  59.   })
  60. ]))
  61. /**
  62. * 使用安全列表,在UnoCSS中使用动态生成的className名称
  63. * 提前声明那些动态生成的类名,以确保它们在最终的CSS中被包含
  64. * 返回数据格式
  65. * [
  66. *   'i-menu:home',
  67. *   'i-custom:welcome',
  68. *   'i-custom:refresh',
  69. *   'i-custom:phone',
  70. *   'i-custom:logout',
  71. *   'i-custom:lock',
  72. *   'i-custom:loading',
  73. *   'i-custom:juejin',
  74. *   'i-custom:home',
  75. *   'i-custom:full-screen',
  76. *   'i-custom:exit-full',
  77. *   'i-custom:copyright'
  78. * ]
  79. */
  80. const generateSafeList = () => {
  81.   return Object.keys(icons).flatMap((item) => icons[item])
  82. };
  83. export default defineConfig({
  84.   presets: [
  85.     // https://unocss.dev/presets/wind3
  86.     presetWind3(),
  87.     // 预设图标
  88.     presetIcons({
  89.       warn: true,
  90.       prefix: ['i-'],
  91.       // 设置全局图标默认属性
  92.       extraProperties: {
  93.         width: "1em",
  94.         height: "1em",
  95.         display: "inline-block",
  96.       },
  97.       // 注册本地 svg 图标集合
  98.       collections,
  99.     }),
  100.   ],
  101.   safelist: generateSafeList(), // 动态生成 `safelist`
  102. });
复制代码
利用图标

  1. <template>
  2.   <div class="home">
  3.     <h2>home</h2>
  4.     测试preset-icons
  5.     <i class="i-custom:exit-full text-#0033cc text-2xl" />
  6.     <i class="i-menu:refresh" />
  7.     <i class="i-custom:juejin text-red text-[50px]" />
  8.     <i class="i-custom:lock hover:text-sky " />
  9.   </div>
  10. </template>
  11. <script setup lang="ts">
  12. </script>
  13. <style scoped>
  14. </style>
复制代码
  i-: 固定前缀

menu或custom: 文件夹名

exit-full或refresh等: svg文件名

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

魏晓东

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表