• vue3中使用插件vite-plugin-svg-icons


    vue3 + vite 项目中使用svg图标
    插件:vite-plugin-svg-icons

    • 预加载 在项目运行时就生成所有图标,只需操作一次 dom
    • 高性能 内置缓存,仅当文件被修改时才会重新生成

    安装

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

    使用

    • vite.config.ts 中的配置插件
    import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
    
    plugins: [
      createSvgIconsPlugin({
        // 指定缓存文件
        iconDirs: [resolve(process.cwd(), "src/assets/icons/svg")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
      }),
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置 main.ts

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

    封装SvgIcon组件 src/components/SvgIcon

    <template>
      <div v-if="isExter" :style="styleExternalIcon" v-bind="$attrs" class="svg-external-icon svg-icon" />
      <svg v-else :class="svgClass" aria-hidden="true" v-bind="$attrs">
        <use :xlink:href="iconName" />
      </svg>
    </template>
    
    <script setup lang="ts">
    import { isExternal } from '@/utils/validate'
    defineOptions({
      name: 'SvgIcon',
    })
    
    const props = withDefaults(defineProps<{
      iconClass: string,
      className?: string
    }>(), {
      className: ''
    })
    
    const isExter = computed(() => {
      return isExternal(props.className)
    })
    const iconName = computed(() => {
      return `#icon-${props.iconClass}`
    })
    const svgClass = computed(() => {
      if (props.className) {
        return 'svg-icon ' + props.className
      } else {
        return 'svg-icon'
      }
    })
    const styleExternalIcon = computed(() => {
      return {
        mask: `url(${props.iconClass}) no-repeat 50% 50%`,
        '-webkit-mask': `url(${props.iconClass}) no-repeat 50% 50%`
      }
    })
    </script>
    
    <style scoped lang="scss">
    .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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    组件使用

    <svg-icon icon-class="404" />
    
    • 1
  • 相关阅读:
    分布式与微服务概念
    MyBatis select标签
    盘点Sui生态20个值得关注的项目,其中8个已进入测试阶段
    RK3568+Codesys ARM+LINUX硬件平台的软PLC解决方案
    推荐系统工业界顶会论文总结——WSDM 2021
    ResNet架构解析
    华为机试真题 Java 实现【运维日志排序】
    NLP_GPT生成式自回归模型
    跨足泛娱乐:TikTok如何重新定义娱乐产业?
    硬实力和软实力,哪个对测试人来说更重要?
  • 原文地址:https://blog.csdn.net/Anyuegogogo/article/details/133523024