深入浅析vue3+vite中怎么使用svg图标

 3455

svg图片在项目中使用的非常广泛,下面本篇文章带大家介绍一下如何在vue3 + vite 中使用svg图标,希望对大家有所帮助!


深入浅析vue3+vite中怎么使用svg图标


vite-plugin-svg-icons

预加载 在项目运行时就生成所有图标,只需操作一次 dom

高性能 内置缓存,仅当文件被修改时才会重新生成

安装

node version: >=12.0.0 vite version: >=2.0.0

  1. yarn add vite-plugin-svg-icons -D
  2. # or
  3. npm i vite-plugin-svg-icons -D
  4. # or
  5. pnpm install vite-plugin-svg-icons -D

使用

vite.config.ts 中的配置插件

  1. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  2. import path from 'path'
  3.  
  4. export default () => {
  5.   return {
  6.     plugins: [
  7.       createSvgIconsPlugin({
  8.         // 指定需要缓存的图标文件夹
  9.         iconDirs: [path.resolve(process.cwd(), 'src/icons')],
  10.         // 指定symbolId格式
  11.         symbolId: 'icon-[dir]-[name]',
  12.  
  13.         /**
  14.          * 自定义插入位置
  15.          * @default: body-last
  16.          */
  17.         // inject?: 'body-last' | 'body-first'
  18.  
  19.         /**
  20.          * custom dom id
  21.          * @default: __svg__icons__dom__
  22.          */
  23.         // customDomId: '__svg__icons__dom__',
  24.       }),
  25.     ],
  26.   }
  27. }

在 src/main.js 内引入注册脚本

  1. import 'virtual:svg-icons-register'

如何在组件中使用

创建SvgIcon组件

/src/components/SvgIcon/index.vue

  1. <template>
  2.   <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
  3.     <use :xlink:href="symbolId" :fill="props.color" />
  4.   </svg>
  5. </template>
  6.  
  7. <script setup>
  8. import { computed } from 'vue'
  9. const props = defineProps({
  10.   prefix: {
  11.     type: String,
  12.     default: 'icon'
  13.   },
  14.   name: {
  15.     type: String,
  16.     required: true
  17.   },
  18.   color: {
  19.     type: String,
  20.     default: '#333'
  21.   },
  22.   size: {
  23.     type: String,
  24.     default: '1em'
  25.   }
  26. })
  27.  
  28. const symbolId = computed(() => `#${props.prefix}-${props.name}`)
  29. </script>

icons目录结构

  1. # src/icons
  2.  
  3. - icon1.svg
  4. - icon2.svg
  5. - icon3.svg
  6. - dir/icon1.svg

全局注册组件

  1. # src/main.js
  2.  
  3. import { createApp } from 'vue'
  4. import App from './App.vue'
  5. import router from './router'
  6.  
  7. import ElementPlus from 'element-plus'
  8. import 'element-plus/dist/index.css'
  9.  
  10. import svgIcon from "@/components/SvgIcon/index.vue";
  11. import 'virtual:svg-icons-register'
  12.  
  13. createApp(App)
  14.     .use(ElementPlus)
  15.     .use(router)
  16.     .component('svg-icon', svgIcon)
  17.     .mount('#app')

页面使用

  1. <template>
  2.     <svg-icon v-if="props.icon" :name="props.icon" />
  3.     <span v-if="props.title" slot='title'>{{ props.title }}</span>
  4. </template>
  5.  
  6. <script setup>
  7.  
  8. const props = defineProps({
  9.     icon: {
  10.         type: String,
  11.         default: ''
  12.     },
  13.     title: {
  14.         type: String,
  15.         default: ''
  16.     }
  17. })
  18. </script>

获取所有 SymbolId

  1. import ids from 'virtual:svg-icons-names'
  2. // => ['icon-icon1','icon-icon2','icon-icon3']


TAG标签:
本文网址:https://www.zztuku.com/detail-11795.html
站长图库 - 深入浅析vue3+vite中怎么使用svg图标
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐