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實(shí)現(xiàn)平滑過(guò)渡的拖拽排序功能
這篇文章主要介紹了vue實(shí)現(xiàn)平滑過(guò)渡的拖拽排序功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
vue.js中created()與activated()的個(gè)人使用解讀
這篇文章主要介紹了vue.js中created()與activated()的個(gè)人使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
基于vue實(shí)現(xiàn)swipe分頁(yè)組件實(shí)例
本篇文章主要介紹了基于vue實(shí)現(xiàn)swipe分頁(yè)組件實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
el-select 下拉框多選實(shí)現(xiàn)全選的實(shí)現(xiàn)
這篇文章主要介紹了el-select 下拉框多選實(shí)現(xiàn)全選的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Vue組件教程之Toast(Vue.extend 方式)詳解
這篇文章主要給大家介紹了關(guān)于Vue組件教程之Toast(Vue.extend 方式)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
使用vue-router切換頁(yè)面時(shí)實(shí)現(xiàn)設(shè)置過(guò)渡動(dòng)畫
今天小編就為大家分享一篇使用vue-router切換頁(yè)面時(shí)實(shí)現(xiàn)設(shè)置過(guò)渡動(dòng)畫。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼
這篇文章主要介紹了Vue無(wú)限滑動(dòng)周選擇日期的組件的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

