• 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

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

    3、配置 tsconfig.json

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

    1. // tsconfig.json
    2. {
    3. "compilerOptions": {
    4. "types": ["vite-plugin-svg-icons/client"]
    5. }
    6. }
    4、封装SvgIcon组件 src/components/SvgIcon
    1. <script>
    2. import { defineComponent, computed } from 'vue'
    3. export default defineComponent({
    4. name: 'SvgIcon',
    5. props: {
    6. prefix: {
    7. type: String,
    8. default: 'icon',
    9. },
    10. iconClass: {
    11. type: String,
    12. required: true,
    13. },
    14. className: {
    15. type: String,
    16. default: ''
    17. },
    18. color: {
    19. type: String,
    20. default: '#333',
    21. },
    22. },
    23. setup(props) {
    24. const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`)
    25. const svgClass = computed(() => {
    26. if (props.className) {
    27. return 'svg-icon ' + props.className
    28. } else {
    29. return 'svg-icon'
    30. }
    31. })
    32. return { symbolId ,svgClass}
    33. },
    34. })
    35. script>
    36. <style scoped>
    37. .svg-icon {
    38. width: 1em;
    39. height: 1em;
    40. vertical-align: -0.15em;
    41. fill: currentColor;
    42. overflow: hidden;
    43. }
    44. .svg-external-icon {
    45. background-color: currentColor;
    46. mask-size: cover!important;
    47. display: inline-block;
    48. }
    49. style>

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

    1. import { App } from "vue";
    2. import SvgIcon from "./SvgIcon";
    3. import "virtual:svg-icons-register";
    4. const svgIconPlugin = {
    5. install(app: App): void {
    6. // 全局挂载
    7. app.component("svg-icon", SvgIcon);
    8. },
    9. };
    10. export default svgIconPlugin;

    6、全局注册 main.ts

    1. import { createApp } from "vue";
    2. import App from "./App.vue";
    3. import router from "./router/router";
    4. // svg封装插件
    5. import svgIcon from "@/components/svgIcon";
    6. createApp(App)
    7. .use(router)
    8. .use(svgIcon)
    9. .mount("#app");

    7、组件中使用 

    1. // 只需name绑定成icons目录下的svg文件名即可
    2. class="heSuan" />

  • 相关阅读:
    《向量数据库指南》——Milvus Cloud和Elastic Cloud 特性对比
    如何消除左递归、何时需要消除左递归—编译原理
    【Java并发编程八】synchronized原理
    特殊版:SpreadJS v15.1 VS SpreadJS v15.0
    TX2 open ttyTHS2
    EtherCAT主站转Ethernet/IP网关
    《统计学习方法》 第二十一章 PageRank算法
    sonarqube的基本使用
    【17.19消失的两个数字】
    hadoop主要组建简写笔记
  • 原文地址:https://blog.csdn.net/hdchangchang/article/details/133846757