vue3+vite項目中顯示SVG圖片的實現(xiàn)
vite-plugin-svg-icons是一個Vite插件,其作用是將SVG圖標文件轉(zhuǎn)換為Vue組件,以便在Vue項目中使用。
使用vite-plugin-svg-icons插件,可以將SVG圖標文件導(dǎo)入到項目中,并將其轉(zhuǎn)換為可復(fù)用的Vue組件。這樣,就可以像使用普通Vue組件一樣使用這些SVG圖標,包括在模板中直接使用、傳遞屬性、綁定事件等。
該插件還提供了一些額外的功能,如自動按需引入圖標、支持圖標的自定義命名、支持指定圖標大小等。
總之,vite-plugin-svg-icons插件的作用是簡化在Vue項目中使用SVG圖標的過程,提供了更加靈活和方便的方式來管理和使用這些圖標。
vite-plugin-svg-icons 安裝
node version: >=12.0.0
vite version: >=2.0.0
npm i vite-plugin-svg-icons -D // 或者 yarn add vite-plugin-svg-icons -D // 或者 pnpm install vite-plugin-svg-icons -D
配置使用vite-plugin-svg-icons
- vite.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__', }), ], } }
- 在 src/main.ts 內(nèi)引入注冊腳本
import 'virtual:svg-icons-register'
到這里 svg 已經(jīng)引入可以使用
如何在Vue 組件使用
/src/components/SvgIcon.vue
<template> <svg aria-hidden="true"> <use :xlink:href="symbolId" rel="external nofollow" :fill="color" /> </svg> </template> <script> import { defineComponent, computed } from 'vue' export default defineComponent({ name: 'SvgIcon', props: { prefix: { type: String, default: 'icon', }, name: { type: String, required: true, }, color: { type: String, default: '#333', }, }, setup(props) { const symbolId = computed(() => `#${props.prefix}-${props.name}`) return { symbolId } }, }) </script>
icons 目錄結(jié)構(gòu)
# src/icons - icon1.svg - icon2.svg - icon3.svg - dir/icon1.svg
/src/App.vue
<template> <div> <SvgIcon name="icon1"></SvgIcon> <SvgIcon name="icon2"></SvgIcon> <SvgIcon name="icon3"></SvgIcon> <SvgIcon name="dir-icon1"></SvgIcon> </div> </template> <script> import { defineComponent, computed } from 'vue' import SvgIcon from './components/SvgIcon.vue' export default defineComponent({ name: 'App', components: { SvgIcon }, }) </script>
優(yōu)點
可擴展性:vite-plugin-svg-icons 提供了靈活的配置選項,允許開發(fā)者自定義圖標的加載和使用方式。
輕量級:vite-plugin-svg-icons 是一個輕量級的插件,沒有額外的依賴,可以快速集成到現(xiàn)有的項目中。
性能優(yōu)化:vite-plugin-svg-icons 可以將 SVG 圖標轉(zhuǎn)換為內(nèi)聯(lián)的 Vue 組件,減少 HTTP 請求,提高頁面加載速度。
簡化開發(fā)流程:使用 vite-plugin-svg-icons,開發(fā)者可以直接在代碼中引用 SVG 圖標,無需手動處理 SVG 文件。
兼容性:vite-plugin-svg-icons 支持多種類型的 SVG 圖標,包括普通的 SVG 文件、Symbol 圖標和圖標字體等。
到此這篇關(guān)于vue3+vite項目中顯示SVG圖片的實現(xiàn)的文章就介紹到這了,更多相關(guān)vue3 顯示SVG圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3的provide和inject實現(xiàn)多級傳遞的原理解析
Vue3中的provide和inject函數(shù)通過原型鏈實現(xiàn)數(shù)據(jù)的多級傳遞,父組件使用provide注入數(shù)據(jù),子組件和后代組件通過inject獲取這些數(shù)據(jù),在創(chuàng)建組件實例時,子組件會繼承父組件的provides屬性對象,介紹Vue3的provide和inject實現(xiàn)多級傳遞的原理,需要的朋友可以參考下2024-12-12Vue?+?SpringBoot?實現(xiàn)文件的斷點上傳、秒傳存儲到Minio的操作方法
這篇文章主要介紹了Vue?+?SpringBoot?實現(xiàn)文件的斷點上傳、秒傳存儲到Minio的操作方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-06-06關(guān)于Vue.js一些問題和思考學(xué)習(xí)筆記(1)
這篇文章主要為大家分享了關(guān)于Vue.js一些問題和思考的學(xué)習(xí)筆記,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12vue3中ts語法使用element plus分頁組件警告錯誤問題
這篇文章主要介紹了vue3中ts語法使用element plus分頁組件警告錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04