vue3+vite+ts?通過svg-sprite-loader?插件使用自定義圖標(biāo)的詳細(xì)步驟
第一步
首先下載svg插件和fs模塊; 后續(xù)需要用到
npm install svg-sprite-loader -D npm install fs
第二步新建文件夾和文件
將下載好的svg文件放入新建好的svg文件夾中
index.vue 代碼 這里是創(chuàng)建一個<svg-icon />
組件
<template> <svg :class="svgClass" v-bind="$attrs" :style="{ color: color }"> <use :xlink:href="iconName"></use> </svg> </template> <script setup lang="ts"> import { computed, defineProps } from "vue"; const props = defineProps({ name: { type: String, required: true, }, color: { type: String, default: "", }, }); const iconName = computed(() => `#icon-${props.name}`); const svgClass = computed(() => { if (props.name) return `svg-icon icon-${props.name}`; return "svg-icon"; }); </script> <style scoped> .svg-icon { width: 1em; height: 1em; fill: currentColor; vertical-align: middle; } </style>
index.ts 代碼
import { readFileSync, readdirSync } from "fs"; let idPerfix = ""; const svgTitle = /<svg([^>+].*?)>/; const clearHeightWidth = /(width|height)="([^>+].*?)"/g; const hasViewBox = /(viewBox="[^>+].*?")/g; const clearReturn = /(\r)|(\n)/g; // 查找svg文件 function svgFind(e) { const arr = []; const dirents = readdirSync(e, { withFileTypes: true }); for (const dirent of dirents) { if (dirent.isDirectory()) arr.push(...svgFind(e + dirent.name + "/")); else { const svg = readFileSync(e + dirent.name) .toString() .replace(clearReturn, "") .replace(svgTitle, ($1, $2) => { let width = 0, height = 0, content = $2.replace(clearHeightWidth, (s1, s2, s3) => { if (s2 === "width") width = s3; else if (s2 === "height") height = s3; return ""; }); if (!hasViewBox.test($2)) content += `viewBox="0 0 ${width} ${height}"`; return `<symbol id="${idPerfix}-${dirent.name.replace( ".svg", "" )}" ${content}>`; }) .replace("</svg>", "</symbol>"); arr.push(svg); } } return arr; } // 生成svg export const createSvg = (path: any, perfix = "icon") => { if (path === "") return; idPerfix = perfix; const res = svgFind(path); return { name: "svg-transform", transformIndexHtml(dom: String) { return dom.replace( "<body>", `<body><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position: absolute; width: 0; height: 0">${res.join( "" )}</svg>` ); }, }; };
第三步 打開main.ts 將創(chuàng)建好的<svg-icon />組件注入到全局組件
import { createApp } from "vue"; import svgIcon from "@/icons/index.vue"; const app = createApp(App); app.component("svg-icon", svgIcon); app.mount("#app");
第四步 在根目錄打開vite.config.ts
import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; import path from "path"; // 引入icons文件夾中的index.ts文件 import { createSvg } from "./src/icons/index"; // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue(), createSvg("./src/icons/svg/")], resolve: { alias: { "@": path.resolve("./src"), // 相對路徑別名配置,使用 @ 代替 src }, } });
**
注意
如果引入的文件爆紅,“該目錄不在項目的文件列表中,項目必須列出所有文件,或使用 “include” 模式。”
解決方案:打開根目錄下的tsconfig.node.json文件的inclue中添加"src/icons/"
完整代碼: “include”: [“vite.config.ts”, "src/icons/",]
**
最后使用
<svg-icon name="hamburger" width="20" height="20" />
引入成功
到此這篇關(guān)于vue3+vite+ts 通過svg-sprite-loader 插件使用自定義圖標(biāo)的文章就介紹到這了,更多相關(guān)vue3+vite+ts 自定義圖標(biāo)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VueTreeselect?參數(shù)options的數(shù)據(jù)轉(zhuǎn)換-參數(shù)normalizer解析
這篇文章主要介紹了VueTreeselect?參數(shù)options的數(shù)據(jù)轉(zhuǎn)換-參數(shù)normalizer解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07vue2使用wangeditor實現(xiàn)數(shù)學(xué)公式和富文本編輯器
這篇文章主要為大家詳細(xì)介紹了vue2如何使用wangeditor實現(xiàn)數(shù)學(xué)公式和富文本編輯器功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-12-12Vue3中Composition?API和Options?API的區(qū)別
Vue3的Composition API和Options API是Vue.js框架中的兩種不同的API,本文主要介紹了Vue3中Composition?API和Options?API的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06vue3+ElementPlus+VueCropper實現(xiàn)上傳圖片功能
文章介紹了如何在Vue3、ElementPlus和VueCropper組件中實現(xiàn)圖片上傳和裁剪功能,包括放大、縮小等操作,感興趣的朋友跟隨小編一起看看吧2025-01-01