亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
vite-plugin-svg-icons
首頁 web前端 Vue.js 深入淺析vue3+vite中怎麼使用svg圖標

深入淺析vue3+vite中怎麼使用svg圖標

Apr 28, 2022 am 10:48 AM
vue svg vue3 vite

svg圖片在專案中使用的非常廣泛,以下這篇文章帶大家介紹如何在vue3 vite 中使用svg圖標,希望對大家有所幫助!

深入淺析vue3+vite中怎麼使用svg圖標

vite-plugin-svg-icons

  • 預(yù)先載入?在專案執(zhí)行時就產(chǎn)生所有圖示,只需操作一次dom
  • 高效能?內(nèi)建快取,僅當檔案被修改時才會重新產(chǎn)生

(學習影片分享: vuejs教學

安裝

node version: ?>=12.0 .0 vite version: ?>=2.0.0

#
yarn add vite-plugin-svg-icons -D
# or
npm i vite-plugin-svg-icons -D
# or
pnpm install vite-plugin-svg-icons -D

使用

  • vite .config.ts 中的設(shè)定外掛
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

export default () => {
  return {
    plugins: [
      createSvgIconsPlugin({
        // 指定需要緩存的圖標文件夾
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',

        /**
         * 自定義插入位置
         * @default: body-last
         */
        // inject?: 'body-last' | 'body-first'

        /**
         * custom dom id
         * @default: __svg__icons__dom__
         */
        // customDomId: '__svg__icons__dom__',
      }),
    ],
  }
}
  • 在src/main.js 內(nèi)引入註冊腳本
import 'virtual:svg-icons-register'

##如何在元件中使用

  • 建立SvgIcon元件

/src/components/SvgIcon/index. vue

<template>
  <svg aria-hidden="true" class="svg-icon" :width="props.size" :height="props.size">
    <use :xlink:href="symbolId" :fill="props.color" />
  </svg>
</template>

<script setup>
import { computed } from &#39;vue&#39;
const props = defineProps({
  prefix: {
    type: String,
    default: &#39;icon&#39;
  },
  name: {
    type: String,
    required: true
  },
  color: {
    type: String,
    default: &#39;#333&#39;
  },
  size: {
    type: String,
    default: &#39;1em&#39;
  }
})

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

  • icons目錄結(jié)構(gòu)

  • #
    # src/icons
    
    - icon1.svg
    - icon2.svg
    - icon3.svg
    - dir/icon1.svg
  • ##全域註冊元件

    # src/main.js
    
    import { createApp } from 'vue'
    import App from './App.vue'
    import router from './router'
    
    import ElementPlus from 'element-plus'
    import 'element-plus/dist/index.css'
    
    import svgIcon from "@/components/SvgIcon/index.vue";
    import &#39;virtual:svg-icons-register&#39;
    
    createApp(App)
        .use(ElementPlus)
        .use(router)
        .component('svg-icon', svgIcon)
        .mount('#app')
  • 頁面使用

    <template>
        <svg-icon v-if="props.icon" :name="props.icon" />
        <span v-if="props.title" slot=&#39;title&#39;>{{ props.title }}</span>
    </template>
    
    <script setup>
    
    const props = defineProps({
        icon: {
            type: String,
            default: &#39;&#39;
        },
        title: {
            type: String,
            default: &#39;&#39;
        }
    })
    </script>

取得所有SymbolId

import ids from &#39;virtual:svg-icons-names&#39;
// => [&#39;icon-icon1&#39;,&#39;icon-icon2&#39;,&#39;icon-icon3&#39;]
(學習影片分享:web前端開發(fā)

程式設(shè)計入門

以上是深入淺析vue3+vite中怎麼使用svg圖標的詳細內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔相應(yīng)的法律責任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

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

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
Vue的反應(yīng)性轉(zhuǎn)換(實驗,然後被刪除)的意義是什麼? Vue的反應(yīng)性轉(zhuǎn)換(實驗,然後被刪除)的意義是什麼? Jun 20, 2025 am 01:01 AM

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

如何在VUE應(yīng)用程序中實施國際化(I18N)和本地化(L10N)? 如何在VUE應(yīng)用程序中實施國際化(I18N)和本地化(L10N)? Jun 20, 2025 am 01:00 AM

國際化和傾斜度invueAppsareprimandermedusingthevuei18nplugin.1.installvue-i18nvianpmoryarn.2.createlo calejsonfiles(例如,en.json,es.json)fortranslationMessages.3.setupthei18ninstanceinmain.jswithlocaleconfigurationandmessagefil

什麼是元素,如何將其用於嵌入向量圖形? 什麼是元素,如何將其用於嵌入向量圖形? Jun 20, 2025 am 10:53 AM

SVG(ScalableVectorGraphics)是一種基於文本的矢量圖形格式,可直接嵌入HTML中實現(xiàn)高質(zhì)量圖形展示。 1.SVG以XML格式存儲,通過標籤定義形狀、顏色和位置,支持圓、矩形、路徑等複雜圖形繪製;2.可通過內(nèi)聯(lián)代碼、標籤、CSS背景圖、或等多種方式嵌入HTML;3.相較於PNG或JPEG,SVG具有分辨率無關(guān)性、文件體積小、可編輯性強、支持CSS與JavaScript交互等優(yōu)勢;4.使用時建議設(shè)置寬高屬性、優(yōu)化可訪問性、測試跨設(shè)備兼容性並利用開發(fā)工具進行調(diào)試。

VUE中的服務(wù)器端渲染SSR是什麼? VUE中的服務(wù)器端渲染SSR是什麼? Jun 25, 2025 am 12:49 AM

Server-Serdendering(SSR)InvueImProvesperformandSeobyGeneratingHtmlonTheserver.1.TheserverrunsvueApcodeAmpCodeAndGeneratesHtmlbBasedonThecurrentRoute.2.thathtmlssenttothebrowserimmed.3.vuehirative eveirtive eveirtive eveirtive eveirtive eveirtive eveirtive eveirtive eveirtiveThepage evepage evepage

如何在VUE中實現(xiàn)過渡和動畫? 如何在VUE中實現(xiàn)過渡和動畫? Jun 24, 2025 pm 02:17 PM

ToaddtransitionsandanimationsinVue,usebuilt-incomponentslikeand,applyCSSclasses,leveragetransitionhooksforcontrol,andoptimizeperformance.1.WrapelementswithandapplyCSStransitionclasseslikev-enter-activeforbasicfadeorslideeffects.2.Useforanimatingdynam

vue中NextTick函數(shù)的目的是什麼?何時需要? vue中NextTick函數(shù)的目的是什麼?何時需要? Jun 19, 2025 am 12:58 AM

nextTick在Vue中用於在DOM更新後執(zhí)行代碼。當數(shù)據(jù)變化時,Vue不會立即更新DOM,而是將其放入隊列,在下一個事件循環(huán)“tick”中處理,因此若需訪問或操作更新後的DOM,應(yīng)使用nextTick;常見場景包括:訪問更新後的DOM內(nèi)容、與依賴DOM狀態(tài)的第三方庫協(xié)作、基於元素尺寸進行計算;其使用方式包括作為組件方法調(diào)用this.$nextTick、導入後單獨使用、結(jié)合async/await;注意事項有:避免過度使用、多數(shù)情況下無需手動觸發(fā)、一次nextTick可捕獲多個更新。

如何使用VUE構(gòu)建組件庫? 如何使用VUE構(gòu)建組件庫? Jul 10, 2025 pm 12:14 PM

搭建Vue組件庫需圍繞業(yè)務(wù)場景設(shè)計結(jié)構(gòu),並遵循開發(fā)、測試、發(fā)布的完整流程。 1.結(jié)構(gòu)設(shè)計應(yīng)按功能模塊分類,包括基礎(chǔ)組件、佈局組件和業(yè)務(wù)組件;2.使用SCSS或CSS變量統(tǒng)一主題與樣式;3.統(tǒng)一命名規(guī)範並引入ESLint和Prettier保證代碼風格一致;4.配套文檔站點展示組件用法;5.使用Vite等工具打包為NPM包並配置rollupOptions;6.發(fā)佈時遵循semver規(guī)範管理版本與changelog。

如何在VUE應(yīng)用程序中管理和訪問環(huán)境變量? 如何在VUE應(yīng)用程序中管理和訪問環(huán)境變量? Jun 14, 2025 am 12:22 AM

在Vue應(yīng)用中管理環(huán)境變量需遵循特定規(guī)則並使用.env文件。首先,僅以VUE_APP_為前綴的變量才會暴露給應(yīng)用;其次,不同環(huán)境對應(yīng)不同.env文件,如.env.development、.env.production等;第三,變量在構(gòu)建時注入,無法運行時更改。具體步驟包括:1.在項目根目錄創(chuàng)建.env文件;2.按模式使用對應(yīng)的.env文件,如.env.staging;3.在代碼中通過process.env訪問變量;4.可將變量集中導入config.js統(tǒng)一管理;5.若需多環(huán)境支持,可在packa

See all articles