vue3+vite项目中使用svgIcon

如何在vue2+webpack项目中使用svgIcon?参考:手摸手,带你优雅的使用 icon - 掘金

这篇文章主要介绍如何在vue3项目中优雅的使用图标

1、安装插件

node version: >=12.0.0

vite version: >=2.0.0

npm i vite-plugin-svg-icons -D

 插件地址:GitHub - vbenjs/vite-plugin-svg-icons: Vite Plugin for fast creating SVG sprites.

2、配置 vite.config.ts

//插件引入
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

  plugins: [
    vue(),
    Components({
      // UI库
      resolvers: [ArcoResolver()],
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
      // 指定symbolId格式
      symbolId: "icon-[name]",

      /**
       * 自定义插入位置
       * @default: body-last
       */
      // inject?: 'body-last' | 'body-first'

      /**
       * custom dom id
       * @default: __svg__icons__dom__
       */
      // customDomId: '__svg__icons__dom__',
    }),
  ],

3、配置 tsconfig.json

如果你使用的 Typescript, 可以在 tsconfig.json 配置文件中添加

// tsconfig.json
{
  "compilerOptions": {
    "types": ["vite-plugin-svg-icons/client"]
  }
}
4、封装SvgIcon组件 src/components/SvgIcon
<template>
  <svg :class="svgClass" a aria-hidden="true">
    <use :href="symbolId" />
  </svg>
</template>

<script>
import { defineComponent, computed } from 'vue'

export default defineComponent({
  name: 'SvgIcon',
  props: {
    prefix: {
      type: String,
      default: 'icon',
    },
    iconClass: {
      type: String,
      required: true,
    },
    className: {
      type: String,
      default: ''
    },
    color: {
      type: String,
      default: '#333',
    },
  },
  setup(props) {
    const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`)
    const svgClass = computed(() => { 
      if (props.className) {
        return 'svg-icon ' + props.className
      } else {
        return 'svg-icon'
      }
    })
    return { symbolId ,svgClass}
  },
})
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover!important;
  display: inline-block;
}
</style>

5、同文件夹下创建 index.ts

import { App } from "vue";
import SvgIcon from "./SvgIcon";
import "virtual:svg-icons-register";

const svgIconPlugin = {
  install(app: App): void {
    // 全局挂载
    app.component("svg-icon", SvgIcon);
  },
};

export default svgIconPlugin;

6、全局注册 main.ts

import { createApp } from "vue";
import App from "./App.vue";

import router from "./router/router";

// svg封装插件
import svgIcon from "@/components/svgIcon";
  
createApp(App)
  .use(router)
  .use(svgIcon)              
  .mount("#app");

7、组件中使用 

// 只需name绑定成icons目录下的svg文件名即可

<svg-icon icon-class="heSuan" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值