Vue3中級指南之如何在vite中使用svg圖標(biāo)詳解
前言
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)文章
vue.js中created()與activated()的個人使用解讀
這篇文章主要介紹了vue.js中created()與activated()的個人使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07el-select 下拉框多選實現(xiàn)全選的實現(xiàn)
這篇文章主要介紹了el-select 下拉框多選實現(xiàn)全選的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Vue組件教程之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è)置過渡動畫。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10