最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vite+vue3項(xiàng)目中使用SVG方式

 更新時(shí)間:2023年10月08日 09:04:44   作者:加麻加辣多香菜  
這篇文章主要介紹了vite+vue3項(xiàng)目中使用SVG方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

vite+vue3項(xiàng)目中使用SVG

在開發(fā)過程中發(fā)現(xiàn)在vite+vue3項(xiàng)目中與在vue2項(xiàng)目中使用SVG的方式有所區(qū)別。

下面將詳細(xì)操作記錄下來。

首先我們先來看一下項(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目錄,用來存放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)文章

  • Vue3前置必備JavaScript知識(shí)之從基礎(chǔ)到實(shí)戰(zhàn)進(jìn)階

    Vue3前置必備JavaScript知識(shí)之從基礎(chǔ)到實(shí)戰(zhàn)進(jìn)階

    這篇文章主要介紹了Vue3前置必備JavaScript知識(shí)之從基礎(chǔ)到實(shí)戰(zhàn)進(jìn)階的相關(guān)資料,從變量與常量、模版字符串、對(duì)象操作、解構(gòu)賦值、箭頭函數(shù)、數(shù)組高頻方法、Promise+Async/Await到模塊化,全面夯實(shí)了開發(fā)者的基礎(chǔ),需要的朋友可以參考下
    2026-03-03
  • Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)

    Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)

    這篇文章主要介紹了Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • vuejs事件中心管理組件間的通信詳解

    vuejs事件中心管理組件間的通信詳解

    這篇文章主要為大家詳細(xì)介紹了vuejs事件中心管理組件間的通信,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Vue使用docx和file-saver實(shí)現(xiàn)Word文檔導(dǎo)出功能

    Vue使用docx和file-saver實(shí)現(xiàn)Word文檔導(dǎo)出功能

    在實(shí)際的前端項(xiàng)目中,經(jīng)常需要將數(shù)據(jù)或頁面內(nèi)容導(dǎo)出為Word文檔,利用docx和file-saver庫,可以實(shí)現(xiàn)強(qiáng)大的文檔導(dǎo)出功能,所以本文介紹了如何使用docx和file-saver庫在前端項(xiàng)目中導(dǎo)出Word文檔,需要的朋友可以參考下
    2026-01-01
  • el-descriptions引入代碼中l(wèi)abel不生效問題及解決

    el-descriptions引入代碼中l(wèi)abel不生效問題及解決

    這篇文章主要介紹了el-descriptions引入代碼中l(wèi)abel不生效問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue時(shí)間選擇控件的使用方式

    vue時(shí)間選擇控件的使用方式

    這篇文章主要介紹了vue時(shí)間選擇控件的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue之延時(shí)刷新實(shí)例

    vue之延時(shí)刷新實(shí)例

    今天小編就為大家分享一篇vue之延時(shí)刷新實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 如何查看vue項(xiàng)目的node版本

    如何查看vue項(xiàng)目的node版本

    文章總結(jié):查看Vue項(xiàng)目中使用的Node版本,特別是當(dāng)項(xiàng)目使用Yarn和TypeScript時(shí),可以通過查看yarn.lock文件中的@types/node@version來確定版本
    2025-01-01
  • 詳解Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)

    詳解Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)

    這篇文章主要介紹了Vue、element-ui、axios實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vite的HMR熱更新突然失效的問題解決

    Vite的HMR熱更新突然失效的問題解決

    Vite是現(xiàn)代前端開發(fā)中的新寵,因其高效的HMR機(jī)制受到開發(fā)者的青睞,但HMR也可能遇到異常情況,下面就來了解一下HMR熱更新突然失效的問題解決,感興趣的可以了解一下
    2026-05-05

最新評(píng)論

容城县| 肇州县| 镇坪县| 鲁甸县| 福建省| 平罗县| 赤壁市| 弥渡县| 寿光市| 永吉县| 博爱县| 惠来县| 囊谦县| 玉门市| 阿拉尔市| 蓬安县| 江城| 军事| 马边| 琼中| 高陵县| 白银市| 阳曲县| 鲜城| 浑源县| 平江县| 蒙山县| 新干县| 大方县| 治县。| 南皮县| 太原市| 永川市| 太谷县| 西安市| 高陵县| 桓仁| 河南省| 泾源县| 高平市| 沅江市|