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

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ā)過(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)文章

最新評(píng)論