Home  >  Article  >  Web Front-end  >  An in-depth analysis of how to use svg icons in vue3+vite

An in-depth analysis of how to use svg icons in vue3+vite

青灯夜游
青灯夜游Original
2022-04-28 10:48:446133browse

svg images are widely used in projects. The following article will introduce how to use svg icons in vue3 vite. I hope it will be helpful to everyone!

An in-depth analysis of how to use svg icons in vue3+vite

vite-plugin-svg-icons

  • Preloading is done when the project is running Generate all icons, only need to operate dom once
  • High performance Built-in cache, only when the file is modified

(Learning video sharing: vuejs tutorial)

Installation

##node version: >=12.0 .0 vite version: >=2.0.0

yarn add vite-plugin-svg-icons -D
# or
npm i vite-plugin-svg-icons -D
# or
pnpm install vite-plugin-svg-icons -D

Use

##vite Configuration plug-in in .config.ts
  • import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
    import path from 'path'
    
    export default () => {
      return {
        plugins: [
          createSvgIconsPlugin({
            // 指定需要缓存的图标文件夹
            iconDirs: [path.resolve(process.cwd(), 'src/icons')],
            // 指定symbolId格式
            symbolId: 'icon-[dir]-[name]',
    
            /**
             * 自定义插入位置
             * @default: body-last
             */
            // inject?: 'body-last' | 'body-first'
    
            /**
             * custom dom id
             * @default: __svg__icons__dom__
             */
            // customDomId: '__svg__icons__dom__',
          }),
        ],
      }
    }
Introduce the registration script in src/main.js
  • import 'virtual:svg-icons-register'

How to add a component Use

  • to create the SvgIcon component

  • /src/components/SvgIcon/index. vue
<template>
  <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
    <use :xlink:href="symbolId" :fill="props.color" />
  </svg>
</template>

<script setup>
import { computed } from &#39;vue&#39;
const props = defineProps({
  prefix: {
    type: String,
    default: &#39;icon&#39;
  },
  name: {
    type: String,
    required: true
  },
  color: {
    type: String,
    default: &#39;#333&#39;
  },
  size: {
    type: String,
    default: &#39;1em&#39;
  }
})

const symbolId = computed(() => `#${props.prefix}-${props.name}`)
</script>

  • icons directory structure

    # src/icons
    
    - icon1.svg
    - icon2.svg
    - icon3.svg
    - dir/icon1.svg
  • Global registration component

    # src/main.js
    
    import { createApp } from 'vue'
    import App from './App.vue'
    import router from './router'
    
    import ElementPlus from 'element-plus'
    import 'element-plus/dist/index.css'
    
    import svgIcon from "@/components/SvgIcon/index.vue";
    import &#39;virtual:svg-icons-register&#39;
    
    createApp(App)
        .use(ElementPlus)
        .use(router)
        .component('svg-icon', svgIcon)
        .mount('#app')
  • Page uses

    <template>
        <svg-icon v-if="props.icon" :name="props.icon" />
        <span v-if="props.title" slot=&#39;title&#39;>{{ props.title }}</span>
    </template>
    
    <script setup>
    
    const props = defineProps({
        icon: {
            type: String,
            default: &#39;&#39;
        },
        title: {
            type: String,
            default: &#39;&#39;
        }
    })
    </script>

to get all SymbolIds

import ids from &#39;virtual:svg-icons-names&#39;
// => [&#39;icon-icon1&#39;,&#39;icon-icon2&#39;,&#39;icon-icon3&#39;]
(Learning video sharing:

web front-end development

, Introduction to programming)

The above is the detailed content of An in-depth analysis of how to use svg icons in vue3+vite. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn