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

vue3+vite2中使用svg的方法詳解(親測(cè)可用)

 更新時(shí)間:2022年08月09日 15:29:05   作者:zasulan  
vue2的時(shí)候使用的是字體圖標(biāo),缺點(diǎn)就是比較單一,到了vue3,相信瀏覽器的性能起來,所以這里記錄一下,下面這篇文章主要給大家介紹了關(guān)于vue3+vite2中使用svg的相關(guān)資料,需要的朋友可以參考下

技術(shù)棧:vue3+vite2

前言:

寫過一版基于vue-cli中使用svg的方法,但是因?yàn)閣ebpack提供了require.context()在vite中無法使用,所以基于vite構(gòu)建的項(xiàng)目則采取另一種方法

一、安裝vite-plugin-svg-icons

此處還需要安裝下fast-glob相關(guān)依賴,不然vite運(yùn)行npm run dev時(shí)會(huì)報(bào)Cannot find module 'fast-glob’的錯(cuò)誤

npm i fast-glob@3.x -D
npm i vite-plugin-svg-icons@2.x -D

二、在src/components/svgIcon下新建組件index.vue

<template>
  <svg aria-hidden="true" class="svg-icon">
    <use :xlink:href="symbolId" rel="external nofollow"  :fill="color" />
  </svg>
</template>

<script setup lang="ts">
import { computed } from 'vue';

const props = defineProps({
  prefix: {type: String,default: 'icon',},
  iconClass: {type: String,required: true,},
  color: {type: String,default: ''}
})

const symbolId = computed(() => `#${props.prefix}-${props.iconClass}`);
</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  overflow: hidden;
  fill: currentColor;
}
</style>

三、tsconfig.json中添加設(shè)置

types用來指定需要包含的模塊,只有在這里列出的模塊的聲明文件才會(huì)被加載進(jìn)來。非必要添加,我在兩個(gè)demo測(cè)試的時(shí)候,一個(gè)需要一個(gè)不需要,若有問題可以嘗試添加

{
  "compilerOptions": {
    "types": ["vite-plugin-svg-icons/client"]
  }
}

四、vite.config.ts 中的配置插件

import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default defineConfig({
  plugins: [
    createSvgIconsPlugin({
      // 指定需要緩存的圖標(biāo)文件夾
      iconDirs: [resolve(process.cwd(), 'src/assets/imgs/svg')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]',
    })
  ]
})

五、在main.ts全局注冊(cè)組件

import { createApp } from 'vue'
import App from './App.vue'
import router from '@/router'
import { store, key } from '@/store'

const app = createApp(App)

import 'virtual:svg-icons-register' // 引入注冊(cè)腳本
import SvgIcon from '@/components/svgIcon/index.vue' // 引入組件
app.component('svg-icon', SvgIcon)

app.use(router).use(store, key).mount('#app')

六、在頁(yè)面中使用

<template>
  <svg-icon icon-class="category"></svg-icon>
  <svg-icon icon-class="accountant" style="width: 10em; height: 10em;border: 1px solid #000000;"></svg-icon>
</template>

七、文件目錄結(jié)構(gòu)及其效果展示

八、參考鏈接地址

1、依賴官方參考文檔:https://github.com/vbenjs/vite-plugin-svg-icons/blob/main/README.zh_CN.md

2、其中有一些內(nèi)容點(diǎn)我根據(jù)該文章進(jìn)行參考:https://www.cnblogs.com/haoxianrui/archive/2022/04/02/16090029.html

3、在vue-cli中使用svg的可以參考我另一篇文章:http://m.fzitv.net/article/258653.htm

總結(jié)

到此這篇關(guān)于vue3+vite2中使用svg的文章就介紹到這了,更多相關(guān)vue3+vite2使用svg內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

乡城县| 千阳县| 黎川县| 南京市| 武定县| 宜宾县| 齐河县| 侯马市| 友谊县| 萍乡市| 阜康市| 拉萨市| 平谷区| 武清区| 墨竹工卡县| 洪洞县| 原阳县| 阳城县| 宁津县| 青河县| 拜泉县| 卢湾区| 遵义市| 长岭县| 原平市| 新津县| 镇康县| 津南区| 明溪县| 湘乡市| 衡南县| 天祝| 三台县| 遵化市| 桦甸市| 蓬莱市| 黔西县| 嘉黎县| 宝兴县| 军事| 新泰市|