vite+typescript+vue集成vite-plugin-svg-icons插件

vite+typescript+vue集成vite-plugin-svg-icons插件

1、安装插件

npm install vite-plugin-svg-icons -D

2、插件配置

vit.config.ts中添加插件配置

// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import path from 'path';


// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    createSvgIconsPlugin({
      iconDirs:[path.resolve(__dirname,'src/assets/icons')], //SVG 图标目录
      symbolId: 'icon-[dir]-[name]' //符号ID格式
    })
  ],
})

3、引入注册脚本

main.ts文件引入注册脚本

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

import 'virtual:svg-icons-register';

const app = createApp(App)

app.mount('#app')

如果出现无法找到模块virtual:svg-icons-register
tsconfig.json中配置

// 确保 typescript扫描了声明文件vite-plugin-svg-icons/client.d.ts 
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue","node_modules/vite-plugin-svg-icons/client.d.ts"]

4、封装组件SvgIcon

<template>
  <svg class="icon" aria-hidden="true" :style="styles">
    <use :href="`#icon-${name}`"></use>
  </svg>
</template>

<script lang="ts" setup>
import { computed } from 'vue';

const props = defineProps({
  name:{ 
    type: String,
    required: true,
   },
  height:{
    type: Number,
    default: 16
  },
  width:{
    type: Number,
    default: 16
  },
  color:{
    type: String,
  }
})
const styles = computed(()=>({
  width: `${props.width}px`,
  height: `${props.height}px`,
  color: props.color
}))
</script>

<style>
.icon {
  width: 1em;
  height: 1em;
  fill: currentColor;
}
</style>

5、测试

App.vue

<script setup lang="ts">
import SvgIcon from './components/SvgIcon.vue';
</script>
<template>
  <div>
    <SvgIcon :name="'vue'"   :height="200" :width="200"/>
  </div>
</template>

效果
在这里插入图片描述
示例项目地址
https://github.com/ALiuYiLin/example-vite-plugin-svg.git

要安装和使用vite-plugin-svg-icons插件,您需要按照以下步骤操作: 1. 打开终端,并确保您的项目已经初始化,并且已经安装了Node.js(版本大于等于12.0.0)和包管理工具(如Yarn或NPM)。 2. 在终端中运行以下命令来安装vite-plugin-svg-icons插件和fast-glob依赖: ``` npm install vite-plugin-svg-icons fast-glob --save-dev ``` 或者使用Yarn: ``` yarn add vite-plugin-svg-icons fast-glob -D ``` 3. 在您的vite.config.ts(或vite.config.js)文件中,导入vite-plugin-svg-icons插件: ```typescript import viteSvgIcons from 'vite-plugin-svg-icons' ``` 4.plugins配置项中添加viteSvgIcons插件: ```typescript export default { // ...其他配置项 plugins: [ viteSvgIcons() ] } ``` 5. 现在,您可以在需要使用svg图标的组件中使用它们了。例如,在您的页面模板中: ```html <template> <svg-icon :name="nameVal" color="blue" class-name="menu-svg-icon" :icon-title="iconTitle" ></svg-icon> </template> ``` 6. 您还可以在脚本中设置和使用其他变量,例如: ```javascript <script setup> import { ref } from 'vue' let iconTitle = ref('svg图片') let nameVal = ref('layer') </script> ``` 7. 最后,您可以根据需要在样式表中自定义图标的样式: ```html <style scoped> .menu-svg-icon { width: 180px; height: 180px; color: red !important; } </style> ``` 请注意,这只是vite-plugin-svg-icons插件的基本安装和使用方式。您还可以根据具体需求进行更多的配置和定制。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值