vite+vue3項目中使用SVG方式
vite+vue3項目中使用SVG
在開發(fā)過程中發(fā)現(xiàn)在vite+vue3項目中與在vue2項目中使用SVG的方式有所區(qū)別。
下面將詳細(xì)操作記錄下來。
首先我們先來看一下項目目錄。
stept1
安裝svg-sprite-loader
yarn add svg-sprite-loader -D
stept2
在components目錄下創(chuàng)建SvgIcon.vue文件
<template> <svg :class="svgClass" v-bind="$attrs" :style="{ color: color }"> <use :xlink:href="iconName" rel="external nofollow" /> </svg> </template> ? <script setup> import { defineProps, computed } from "vue" ? const props = defineProps({ name: { type: String, required: true }, color: { type: String, default: "" } }) ? const iconName = computed(() => `#icon-${props.name}`) const svgClass = computed(() => { console.log(props.name, "props.name") if (props.name) { return `svg-icon ${iconName.value}` } return "svg-icon" }) </script> ? <style scoped> .svg-icon { width: 1em; height: 1em; fill: currentColor; vertical-align: middle; } </style>
stept3
在src目錄下創(chuàng)建icons目錄,用來存放SVG文件
stept4
在main.js中將我們的SvgIcon組件全局注冊
stept5
在src下新建plugins文件夾,創(chuàng)建svgBuilder.js
import { readFileSync, readdirSync } from "fs" ? let idPerfix = "" const svgTitle = /<svg([^>+].*?)>/ const clearHeightWidth = /(width|height)="([^>+].*?)"/g ? const hasViewBox = /(viewBox="[^>+].*?")/g ? const clearReturn = /(\r)|(\n)/g ? function findSvgFile(dir) { const svgRes = [] const dirents = readdirSync(dir, { withFileTypes: true }) for (const dirent of dirents) { if (dirent.isDirectory()) { svgRes.push(...findSvgFile(dir + dirent.name + "/")) } else { const svg = readFileSync(dir + dirent.name) .toString() .replace(clearReturn, "") .replace(svgTitle, ($1, $2) => { // console.log(++i) // console.log(dirent.name) let width = 0 let height = 0 let 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>") svgRes.push(svg) } } return svgRes } ? export const svgBuilder = (path, perfix = "icon") => { if (path === "") return idPerfix = perfix const res = findSvgFile(path) // console.log(res.length) // const res = [] return { name: "svg-transform", transformIndexHtml(html) { return html.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> ` ) } } }
stept6
在vite.config.js修改配置
stept7
在項目中使用,name和color必傳
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏你知道嗎
這篇文章主要為大家詳細(xì)介紹了vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02vue post application/x-www-form-urlencoded如何實現(xiàn)傳參
這篇文章主要介紹了vue post application/x-www-form-urlencoded如何實現(xiàn)傳參問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04詳解如何在Vue3使用<script lang=“ts“ setup>語法糖
本文主要介紹了在Vue3使用<script lang=“ts“ setup>語法糖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Ruoyi-Vue處理跨域問題同時請求多個域名接口(前端處理方法)
跨域問題一直都是很多人比較困擾的問題,這篇文章主要給大家介紹了關(guān)于Ruoyi-Vue處理跨域問題同時請求多個域名接口的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05Vue3使用vue-qrcode-reader實現(xiàn)掃碼綁定設(shè)備功能(推薦)
本文介紹了在Vue3中使用vue-qrcode-reader版本5.5.7來實現(xiàn)移動端的掃碼綁定設(shè)備功能,用戶通過掃描二維碼自動獲取設(shè)備序列號,并填充到添加設(shè)備界面,完成設(shè)備綁定的全過程,包含ScanCode.vue和DeviceAdd.vue兩個主要界面的實現(xiàn)方式2024-10-10