vite+vue3項目中svg圖標組件封裝的過程詳解
一、安裝插件
npm i vite-plugin-svg-icons -D
二、插件配置
// vite.config.js import { createSvgIconsPlugin } from "vite-plugin-svg-icons"; import path from "path"; export default defineConfig({ plugins: [ // svg圖標配置項 createSvgIconsPlugin({ // 指定需要緩存的圖標文件夾 iconDirs: [path.resolve(process.cwd(), "src/assets/icons")], // 指定symbolId格式 symbolId: "icon-[dir]-[name]", }), // ------ 其他配置項 ------ ], });
三、引入注冊腳本
// src/main.js import 'virtual:svg-icons-register'
四、封裝組件
在src/components目錄下創(chuàng)建一個SvgIcon.vue文件
// src/components/SvgIcon.vue <template> <svg aria-hidden="true" :style="{ width: width + 'px', height: height + 'px' }"> <use :xlink:href="symbolId" rel="external nofollow" :fill="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: 'currentColor', }, width: { type: [Number, String], default: '24', }, height: { type: [Number, String], default: '24', }, }) const symbolId = computed(() => `#${props.prefix}-${props.name}`); </script>
五、icons 文件目錄結(jié)構(gòu)
# src/assets/icons - icon1.svg - icon2.svg - icon3.svg - dir/icon1.svg
六、在頁面中使用
<template> <!-- color 不傳值 svg顏色會繼承上級元素color屬性值 --> <svgIcon name="mine" color="#333" width="36" height="36"></svgIcon> </template> <script setup> import svgIcon from "@/components/SvgIcon.vue" </script>
七、svg圖標顏色不生效解決方案
如果svg圖標設(shè)置顏色值不生效,點擊 svg文件 查看源碼,修改 fill 屬性值為 fill=“currentColor”或者 fill="" 。
<?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 24.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 200 200" style="enable-background:new 0 0 200 200;" xml:space="preserve"> <style type="text/css"> .st0{fill:#FFFFFF;} // 就是這里把這個#ffffff改成""或者"currentColor"。 </style> <path class="st0" d="M912.051527 151.150632l-286.624817 780.499041c-5.753719 15.667798-23.118384 23.704707-38.786182 17.950989a30.220937 30.220937 0 0 1-19.305944-22.909364L498.23787 550.442426a30.220937 30.220937 0 0 0-24.265655-24.265655L97.723343 457.080057c-16.415729-3.014425-27.279412-18.766366-24.264987-35.182094a30.220937 30.220937 0 0 1 19.306612-22.910032L873.263342 112.363782c15.669134-5.753719 33.033799 2.28319 38.786849 17.951656a30.220937 30.220937 0 0 1 0 20.835194zM826.833582 205.907791a7.555234 7.555234 0 0 0-9.679684-9.650301l-573.559491 207.092476a7.555234 7.555234 0 0 0 1.149942 14.527205l297.554613 56.790594a7.555234 7.555234 0 0 1 6.020837 6.087616L603.515031 788.626754a7.555234 7.555234 0 0 0 14.549911 1.210044L826.83425 205.908459z" /> </svg>;
到此這篇關(guān)于vite+vue3項目中svg圖標組件封裝的文章就介紹到這了,更多相關(guān)vite vue3封裝svg組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue中el-menu-item實現(xiàn)路由跳轉(zhuǎn)的完整步驟
這篇文章主要給大家介紹了關(guān)于Vue中el-menu-item實現(xiàn)路由跳轉(zhuǎn)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-09-09vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
有時候我們需要對一個組件綁定自定義 v-model,以更方便地實現(xiàn)雙向數(shù)據(jù),例如自定義表單輸入控件,這篇文章主要介紹了vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法,需要的朋友可以參考下2024-07-07vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法
這篇文章主要給大家介紹了關(guān)于vue數(shù)據(jù)更新但視圖(DOM)不刷新的幾種解決辦法,我們在開發(fā)過程中經(jīng)常會碰到數(shù)據(jù)更新,但是視圖并未改變的情況,需要的朋友可以參考下2023-08-08使用vue3+vite導(dǎo)入圖片路徑錯亂問題排查及解決
使用vue3+vite開發(fā)的時候,導(dǎo)入svg圖片時,同一個文件夾下的文件,其中一個路徑正常解析,另一個不行,更改文件名之后,該圖片文件就可以正常解析了,本文給大家介紹了使用vue3+vite導(dǎo)入圖片路徑錯亂問題排查及解決,需要的朋友可以參考下2024-03-03使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端)
這篇文章主要介紹了使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12springboot之springboot與netty整合方案
這篇文章主要介紹了VUE之關(guān)于store狀態(tài)管理核心解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06