欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3中級指南之如何在vite中使用svg圖標(biāo)詳解

 更新時間:2022年04月28日 09:41:10   作者:只會番茄炒蛋  
在以webpack為構(gòu)建工具的開發(fā)環(huán)境中我們可以很方便的實現(xiàn)SVG圖標(biāo)的組件化,下面這篇文章主要給大家介紹了關(guān)于Vue3中級指南之如何在vite中使用svg圖標(biāo)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言

svg圖片在項目中使用的非常廣泛,今天記錄一下我是如何在vue3 + vite 中使用svg圖標(biāo)。

vite-plugin-svg-icons

  • 預(yù)加載 在項目運行時就生成所有圖標(biāo),只需操作一次 dom
  • 高性能 內(nèi)置緩存,僅當(dāng)文件被修改時才會重新生成

安裝

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

使用

  • vite.config.ts 中的配置插件
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

export default () => {
  return {
    plugins: [
      createSvgIconsPlugin({
        // 指定需要緩存的圖標(biāo)文件夾
        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__',
      }),
    ],
  }
}
  • 在 src/main.js 內(nèi)引入注冊腳本
import 'virtual:svg-icons-register'

如何在組件中使用

創(chuàng)建SvgIcon組件

/src/components/SvgIcon/index.vue

<template>
  <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
    <use :xlink:href="symbolId" rel="external nofollow"  :fill="props.color" />
  </svg>
</template>

<script setup>
import { computed } from 'vue'
const props = defineProps({
  prefix: {
    type: String,
    default: 'icon'
  },
  name: {
    type: String,
    required: true
  },
  color: {
    type: String,
    default: '#333'
  },
  size: {
    type: String,
    default: '1em'
  }
})
const symbolId = computed(() => `#${props.prefix}-${props.name}`)
</script>

icons目錄結(jié)構(gòu)

# src/icons

- icon1.svg
- icon2.svg
- icon3.svg
- dir/icon1.svg

全局注冊組件

# 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 'virtual:svg-icons-register'

createApp(App)
    .use(ElementPlus)
    .use(router)
    .component('svg-icon', svgIcon)
    .mount('#app')

頁面使用

<template>
    <svg-icon v-if="props.icon" :name="props.icon" />
    <span v-if="props.title" slot='title'>{{ props.title }}</span>
</template>

<script setup>
const props = defineProps({
    icon: {
        type: String,
        default: ''
    },
    title: {
        type: String,
        default: ''
    }
})
</script>

獲取所有 SymbolId

import ids from 'virtual:svg-icons-names'
// => ['icon-icon1','icon-icon2','icon-icon3']

總結(jié)

到此這篇關(guān)于Vue3中級指南之如何在vite中使用svg圖標(biāo)的文章就介紹到這了,更多相關(guān)vite使用svg圖標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue3中v-model語法糖的三種寫法詳解

    Vue3中v-model語法糖的三種寫法詳解

    這篇文章主要為大家詳細介紹了Vue3中v-model語法糖的三種寫法,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 基于Vue實現(xiàn)平滑過渡的拖拽排序功能

    基于Vue實現(xiàn)平滑過渡的拖拽排序功能

    這篇文章主要介紹了vue實現(xiàn)平滑過渡的拖拽排序功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • vue.js中created()與activated()的個人使用解讀

    vue.js中created()與activated()的個人使用解讀

    這篇文章主要介紹了vue.js中created()與activated()的個人使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 基于vue實現(xiàn)swipe分頁組件實例

    基于vue實現(xiàn)swipe分頁組件實例

    本篇文章主要介紹了基于vue實現(xiàn)swipe分頁組件實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • el-select 下拉框多選實現(xiàn)全選的實現(xiàn)

    el-select 下拉框多選實現(xiàn)全選的實現(xiàn)

    這篇文章主要介紹了el-select 下拉框多選實現(xiàn)全選的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Vue組件教程之Toast(Vue.extend 方式)詳解

    Vue組件教程之Toast(Vue.extend 方式)詳解

    這篇文章主要給大家介紹了關(guān)于Vue組件教程之Toast(Vue.extend 方式)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • 使用vue-router切換頁面時實現(xiàn)設(shè)置過渡動畫

    使用vue-router切換頁面時實現(xiàn)設(shè)置過渡動畫

    今天小編就為大家分享一篇使用vue-router切換頁面時實現(xiàn)設(shè)置過渡動畫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Vue無限滑動周選擇日期的組件的示例代碼

    Vue無限滑動周選擇日期的組件的示例代碼

    這篇文章主要介紹了Vue無限滑動周選擇日期的組件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 學(xué)習(xí)Vue組件實例

    學(xué)習(xí)Vue組件實例

    本篇文章給大家分享了Vue實例的相關(guān)內(nèi)容以及重要知識點,對此有興趣的朋友可以跟著學(xué)習(xí)參考下。
    2018-04-04
  • Vue?element樹形控件添加虛線詳解

    Vue?element樹形控件添加虛線詳解

    這篇文章主要為大家介紹了Vue?element樹形控件添加虛線,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>
    2021-11-11

最新評論