vue3 vite2中怎么使用svg方法
May 11, 2023 pm 05:55 PM一、安裝vite-plugin-svg-icons
此處還需要安裝下fast-glob相關(guān)依賴,不然vite運(yùn)行npm run dev時會報(bào)Cannot find module 'fast-glob’的錯誤
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用來指定需要包含的模塊,只有在這里列出的模塊的聲明文件才會被加載進(jìn)來。非必要添加,我在兩個demo測試的時候,一個需要一個不需要,若有問題可以嘗試添加
{ "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全局注冊組件
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' // 引入注冊腳本 import SvgIcon from '@/components/svgIcon/index.vue' // 引入組件 app.component('svg-icon', SvgIcon) app.use(router).use(store, key).mount('#app')
六、在頁面中使用
<template> <svg-icon icon-class="category"></svg-icon> <svg-icon icon-class="accountant" ></svg-icon> </template>
七、文件目錄結(jié)構(gòu)及其效果展示
以上是vue3 vite2中怎么使用svg方法的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

熱AI工具

Undress AI Tool
免費(fèi)脫衣服圖片

Undresser.AI Undress
人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover
用于從照片中去除衣服的在線人工智能工具。

Clothoff.io
AI脫衣機(jī)

Video Face Swap
使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費(fèi)的代碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級代碼編輯軟件(SublimeText3)

想要實(shí)現(xiàn)頁面的局部刷新,我們只需要實(shí)現(xiàn)局部組件(dom)的重新渲染。在Vue中,想要實(shí)現(xiàn)這一效果最簡便的方式方法就是使用v-if指令。在Vue2中我們除了使用v-if指令讓局部dom的重新渲染,也可以新建一個空白組件,需要刷新局部頁面時跳轉(zhuǎn)至這個空白組件頁面,然后在空白組件內(nèi)的beforeRouteEnter守衛(wèi)中又跳轉(zhuǎn)回原來的頁面。如下圖所示,如何在Vue3.X中實(shí)現(xiàn)點(diǎn)擊刷新按鈕實(shí)現(xiàn)紅框范圍內(nèi)的dom重新加載,并展示對應(yīng)的加載狀態(tài)。由于Vue3.X中scriptsetup語法中組件內(nèi)守衛(wèi)只有o

Vue實(shí)現(xiàn)博客前端,需要實(shí)現(xiàn)markdown的解析,如果有代碼則需要實(shí)現(xiàn)代碼的高亮。Vue的markdown解析庫有很多,如markdown-it、vue-markdown-loader、marked、vue-markdown等。這些庫都大同小異。這里選用的是marked,代碼高亮的庫選用的是highlight.js。具體實(shí)現(xiàn)步驟如下:一、安裝依賴庫在vue項(xiàng)目下打開命令窗口,并輸入以下命令npminstallmarked-save//marked用于將markdown轉(zhuǎn)換成htmlnpmins

vue3+vite:src使用require動態(tài)導(dǎo)入圖片報(bào)錯和解決方法vue3+vite動態(tài)的導(dǎo)入多張圖片vue3如果使用的是typescript開發(fā),就會出現(xiàn)require引入圖片報(bào)錯,requireisnotdefined不能像使用vue2這樣imgUrl:require(’…/assets/test.png’)導(dǎo)入,是因?yàn)閠ypescript不支持require所以用import導(dǎo)入,下面介紹如何解決:使用awaitimport

tinymce是一個功能齊全的富文本編輯器插件,但在vue中引入tinymce并不像別的Vue富文本插件一樣那么順利,tinymce本身并不適配Vue,還需要引入@tinymce/tinymce-vue,并且它是國外的富文本插件,沒有通過中文版本,需要在其官網(wǎng)下載翻譯包(可能需要翻墻)。1、安裝相關(guān)依賴npminstalltinymce-Snpminstall@tinymce/tinymce-vue-S2、下載中文包3.引入皮膚和漢化包在項(xiàng)目public文件夾下新建tinymce文件夾,將下載的

最終效果安裝VueCropper組件yarnaddvue-cropper@next上面的安裝值針對Vue3的,如果時Vue2或者想使用其他的方式引用,請?jiān)L問它的npm官方地址:官方教程。在組件中引用使用時也很簡單,只需要引入對應(yīng)的組件和它的樣式文件,我這里沒有在全局引用,只在我的組件文件中引入import{userInfoByRequest}from'../js/api'import{VueCropper}from'vue-cropper&

vue3+ts+axios+pinia實(shí)現(xiàn)無感刷新1.先在項(xiàng)目中下載aiXos和pinianpmipinia--savenpminstallaxios--save2.封裝axios請求-----下載js-cookienpmiJS-cookie-s//引入aixosimporttype{AxiosRequestConfig,AxiosResponse}from"axios";importaxiosfrom'axios';import{ElMess

前言無論是vue還是react,當(dāng)遇到多處重復(fù)代碼的時候,我們都會想著如何復(fù)用這些代碼,而不是一個文件里充斥著一堆冗余代碼。實(shí)際上,vue和react都可以通過抽組件的方式來達(dá)到復(fù)用,但如果遇到一些很小的代碼片段,你又不想抽到另外一個文件的情況下,相比而言,react可以在相同文件里面聲明對應(yīng)的小組件,或者通過renderfunction來實(shí)現(xiàn),如:constDemo:FC=({msg})=>{returndemomsgis{msg}}constApp:FC=()=>{return(

一、效果展示二、npmnpminstall@vueup/vue-quill@alpha--save三、main.js引入import{QuillEditor}from'@vueup/vue-quill'import'@vueup/vue-quill/dist/vue-quill.snow.css';app.component('QuillEditor',QuillEditor)四、頁面使用{{TiLe
