Vite项目封装SvgIcon组件——vite-plugin-svg-icons

8 篇文章 0 订阅
文章介绍了如何使用vite-plugin-svg-icons插件来高效管理和预加载SVG图标。该插件在项目运行时生成所有图标,并内置缓存,仅在文件修改时更新。用户需要Nodev12.0.0+和Vitev2.0.0+环境。配置包括在vite.config.js中引入插件并设置参数,创建SvgIcon组件,以及在main.js中全局注册。文章还展示了如何在Vue组件中使用这些SVG图标。
摘要由CSDN通过智能技术生成

vite-plugin-svg-icons github地址

Feature

  • Preloading All icons are generated when the project is running, and you only need to operate dom once.
  • High performance Built-in cache, it will be regenerated only when the file is modified.

安装

环境要求

  • node version: >=12.0.0
  • vite version: >=2.0.0
npm i vite-plugin-svg-icons -D
# or
pnpm add -D vite-plugin-svg-icons

vite.config.js 配置

import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  return {
    plugins: [
      /* ... */
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]'
      })
    ]
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        '@': resolve(__dirname, 'src'),
      }
    },
    server: {
      host: '0.0.0.0',
      port: 9881
    },
  }
})

新建 SvgIcon 组件

  • src/components/SvgIcon/index.vue
<!-- vue文件 index.vue -->
<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName" :fill="color" />
  </svg>
</template>

<script setup>
const props = defineProps({
  iconClass: {
    type: String,
    required: true
  },
  className: {
    type: String,
    default: ''
  },
  color: {
    type: String,
    default: ''
  },
});
const iconName = computed(() => `#icon-${props.iconClass}`)
const svgClass = computed(() => props.className ? `svg-icon ${props.className}` : 'svg-icon')
</script>

<style lang="scss" scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  position: relative;
  fill: currentColor;
}
</style>
  • src/components/SvgIcon/svgicon.js

UI组件库用的是 Antd Vue

// svgicon.js
import * as components from '@ant-design/icons-vue'

export default {
  install: (app) => {
    for (const key in components) {
      const componentConfig = components[key];
      app.component(componentConfig.name, componentConfig);
    }
  },
};

main.js 全局注册

  • 导入上面新建的两个文件
// main.js
// svg图标
import 'virtual:svg-icons-register'
import SvgIcon from '@/components/SvgIcon/index.vue'
import antdvueIcons from '@/components/SvgIcon/svgicon.js'
  • 全局注册
// main.js
import { createApp } from 'vue'

const app = createApp(App)
app.component('svg-icon', SvgIcon)

app.use(antdvueIcons)
  .mount('#app')

组件页面使用

<template>
	<svg-icon icon-class="meeting" style="width: 40px; height: 155px;" />
</template>

<script setup>
import { defineAsyncComponent } from 'vue'

const SvgIcon = defineAsyncComponent(() => import('@/components/SvgIcon/index.vue'))
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue3中封装vite-plugin-svg-icons插件并封装SvgIcon组件的步骤如下: 1. 首先安装vite-plugin-svg-icons插件和fast-glob依赖包,可以使用以下命令进行安装: ``` npm i vite-plugin-svg-icons npm i fast-glob ``` 2. 在项目的配置文件中进行插件的配置。具体配置方式可以参考vite-plugin-svg-icons的官方文档。 3. 将SVG图标文件放置在对应的路径下,并与iconDirs设置保持一致。 4. 在Vue组件中使用SvgIcon组件,并传递相应的props。例如,在index.vue文件中可以这样使用SvgIcon组件: ```html <template> <svg-icon :name="nameVal" color="blue" class-name="menu-svg-icon" :icon-title="iconTitle"></svg-icon> </template> <script setup> import { ref } from 'vue'; import SvgIcon from "@/components/SvgIcon.vue"; let iconTitle = ref('svg图片'); let nameVal = ref('layer'); </script> <style scoped> .menu-svg-icon { width: 180px; height: 180px; color: red !important; } </style> ``` 5. 最后,在需要全局引入SvgIcon组件的地方进行引入和使用。 请注意,这只是一个大致的步骤,具体的实现可能会根据项目的需求和配置方式有所差异。你可以根据自己的实际情况进行相应的调整和配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue2+vite中,通过插件vite-plugin-svg-icons和fast-glob封装组件使用svg图片](https://blog.csdn.net/weixin_44867717/article/details/126603027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [【vue3】使用vite-plugin-svg-icons插件显示本地svg图标](https://blog.csdn.net/qq_36977923/article/details/127302147)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值