Vue3中級(jí)指南之如何在vite中使用svg圖標(biāo)詳解
前言
svg圖片在項(xiàng)目中使用的非常廣泛,今天記錄一下我是如何在vue3 + vite 中使用svg圖標(biāo)。
vite-plugin-svg-icons
- 預(yù)加載 在項(xiàng)目運(yùn)行時(shí)就生成所有圖標(biāo),只需操作一次 dom
- 高性能 內(nèi)置緩存,僅當(dāng)文件被修改時(shí)才會(huì)重新生成
安裝
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)引入注冊(cè)腳本
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
全局注冊(cè)組件
# 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')頁(yè)面使用
<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中級(jí)指南之如何在vite中使用svg圖標(biāo)的文章就介紹到這了,更多相關(guān)vite使用svg圖標(biāo)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue結(jié)合leaflet實(shí)現(xiàn)地圖放大鏡
放大鏡在很多地方都可以使用的到,本文主要介紹了vue結(jié)合leaflet實(shí)現(xiàn)地圖放大鏡,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
element-ui樹(shù)形控件后臺(tái)返回的數(shù)據(jù)+生成組織樹(shù)的工具類(lèi)
這篇文章主要介紹了element-ui樹(shù)形控件后臺(tái)返回的數(shù)據(jù)+生成組織樹(shù)的工具類(lèi),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換)
這篇文章主要介紹了關(guān)于VanCascader默認(rèn)值(地址code轉(zhuǎn)換),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue3+pinia用戶信息持久緩存token的問(wèn)題解決
本文主要介紹了vue3+pinia用戶信息持久緩存token的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
簡(jiǎn)單理解vue中實(shí)例屬性vm.$els
這篇文章主要幫助大家簡(jiǎn)單理解vue中實(shí)例屬性vm.$els,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
修改Vue2項(xiàng)目運(yùn)行端口號(hào)配置后端代理教程
文章介紹了在Vue2項(xiàng)目中配置端口號(hào)和代理解決跨域問(wèn)題的方法,包括在vue.config.js文件中增加端口號(hào)配置和設(shè)置代理路徑2026-01-01
vue中transition組件在項(xiàng)目中運(yùn)用小結(jié)
這篇文章主要介紹了vue中transition組件在項(xiàng)目中運(yùn)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
Vue雙向數(shù)據(jù)綁定失效的常見(jiàn)場(chǎng)景和解決方案
在 Vue 2 和 Vue 3 中,雙向數(shù)據(jù)綁定(主要通過(guò)?v-model?實(shí)現(xiàn))可能會(huì)在一些特定場(chǎng)景下失效,這些情況通常與 Vue 的響應(yīng)式系統(tǒng)限制或不正確的用法有關(guān),以下是兩者的主要失效場(chǎng)景和解決辦法,需要的朋友可以參考下2025-09-09

