欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vite+vue3項目中使用SVG方式

 更新時間:2023年10月08日 09:04:44   作者:加麻加辣多香菜  
這篇文章主要介紹了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生命周期、手動掛載及掛載子組件

    深入理解Vue生命周期、手動掛載及掛載子組件

    本篇文章主要介紹了深入理解Vue生命周期和手動掛載,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Vue+Mock.js模擬登錄和表格的增刪改查功能

    Vue+Mock.js模擬登錄和表格的增刪改查功能

    這篇文章主要介紹了Vue+Mock.js模擬登錄和表格的增刪改查功能,需要的朋友可以參考下
    2018-07-07
  • vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏你知道嗎

    vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏你知道嗎

    這篇文章主要為大家詳細(xì)介紹了vue中的eventBus會不會產(chǎn)生內(nèi)存泄漏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • vue post application/x-www-form-urlencoded如何實現(xiàn)傳參

    vue 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>語法糖

    本文主要介紹了在Vue3使用<script lang=“ts“ setup>語法糖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Ruoyi-Vue處理跨域問題同時請求多個域名接口(前端處理方法)

    Ruoyi-Vue處理跨域問題同時請求多個域名接口(前端處理方法)

    跨域問題一直都是很多人比較困擾的問題,這篇文章主要給大家介紹了關(guān)于Ruoyi-Vue處理跨域問題同時請求多個域名接口的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Vue3使用vue-qrcode-reader實現(xiàn)掃碼綁定設(shè)備功能(推薦)

    Vue3使用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
  • Vue中插槽slot的使用方法與應(yīng)用場景詳析

    Vue中插槽slot的使用方法與應(yīng)用場景詳析

    這篇文章主要給大家介紹了關(guān)于Vue中插槽slot的使用方法與應(yīng)用場景的相關(guān)資料,插槽(Slot)是Vue提出來的一個概念,正如名字一樣,插槽用于決定將所攜帶的內(nèi)容,插入到指定的某個位置,從而使模板分塊,具有模塊化的特質(zhì)和更大的重用性,需要的朋友可以參考下
    2021-06-06
  • vue項目使用uniapp生成app的全過程

    vue項目使用uniapp生成app的全過程

    這篇文章主要介紹了vue項目使用uniapp生成app的全過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue進行下載與處理二進制流文件的方法詳解

    vue進行下載與處理二進制流文件的方法詳解

    這篇文章主要為大家詳細(xì)介紹了vue如何實現(xiàn)將后端返回的二進制流進行處理并實現(xiàn)下載,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-12-12

最新評論