vite+vue3項(xiàng)目中使用SVG方式
vite+vue3項(xiàng)目中使用SVG
在開發(fā)過(guò)程中發(fā)現(xiàn)在vite+vue3項(xiàng)目中與在vue2項(xiàng)目中使用SVG的方式有所區(qū)別。
下面將詳細(xì)操作記錄下來(lái)。
首先我們先來(lái)看一下項(xiàng)目目錄。
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目錄,用來(lái)存放SVG文件
stept4
在main.js中將我們的SvgIcon組件全局注冊(cè)
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
在項(xiàng)目中使用,name和color必傳
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue中的eventBus會(huì)不會(huì)產(chǎn)生內(nèi)存泄漏你知道嗎
這篇文章主要為大家詳細(xì)介紹了vue中的eventBus會(huì)不會(huì)產(chǎn)生內(nèi)存泄漏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參
這篇文章主要介紹了vue post application/x-www-form-urlencoded如何實(shí)現(xiàn)傳參問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04詳解如何在Vue3使用<script lang=“ts“ setup>語(yǔ)法糖
本文主要介紹了在Vue3使用<script lang=“ts“ setup>語(yǔ)法糖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Ruoyi-Vue處理跨域問(wèn)題同時(shí)請(qǐng)求多個(gè)域名接口(前端處理方法)
跨域問(wèn)題一直都是很多人比較困擾的問(wèn)題,這篇文章主要給大家介紹了關(guān)于Ruoyi-Vue處理跨域問(wèn)題同時(shí)請(qǐng)求多個(gè)域名接口的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05Vue3使用vue-qrcode-reader實(shí)現(xiàn)掃碼綁定設(shè)備功能(推薦)
本文介紹了在Vue3中使用vue-qrcode-reader版本5.5.7來(lái)實(shí)現(xiàn)移動(dòng)端的掃碼綁定設(shè)備功能,用戶通過(guò)掃描二維碼自動(dòng)獲取設(shè)備序列號(hào),并填充到添加設(shè)備界面,完成設(shè)備綁定的全過(guò)程,包含ScanCode.vue和DeviceAdd.vue兩個(gè)主要界面的實(shí)現(xiàn)方式2024-10-10Vue中插槽slot的使用方法與應(yīng)用場(chǎng)景詳析
這篇文章主要給大家介紹了關(guān)于Vue中插槽slot的使用方法與應(yīng)用場(chǎng)景的相關(guān)資料,插槽(Slot)是Vue提出來(lái)的一個(gè)概念,正如名字一樣,插槽用于決定將所攜帶的內(nèi)容,插入到指定的某個(gè)位置,從而使模板分塊,具有模塊化的特質(zhì)和更大的重用性,需要的朋友可以參考下2021-06-06vue項(xiàng)目使用uniapp生成app的全過(guò)程
這篇文章主要介紹了vue項(xiàng)目使用uniapp生成app的全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue進(jìn)行下載與處理二進(jìn)制流文件的方法詳解
這篇文章主要為大家詳細(xì)介紹了vue如何實(shí)現(xiàn)將后端返回的二進(jìn)制流進(jìn)行處理并實(shí)現(xiàn)下載,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12