Vue3使用icon的兩種方式實(shí)例
技術(shù)棧和版本 Vite2 + Vue3 + fontAwesome5
Vue3 中使用Icon的方式,fontAwesome 簡(jiǎn)單好用,但有時(shí)候缺少想要的icon。采用svg的方式,想要什么,直接下載,然后使用,種類(lèi)更加的全,基本上沒(méi)有不符合需求的icon,但是沒(méi)有fontAwesome 相對(duì)的容易輕松,每次都要下載對(duì)應(yīng)的圖片。
1. 使用svg
a 下載需要使用的SVG圖片,保存至 src/icons文件夾

b 安裝依賴(lài) fs 和svg的loader
命令:npm install svg-sprite-loader
命令:npm install --save fs
c 創(chuàng)建模板文件 index.vue

模板文件代碼
<template>
<svg :class="svgClass" v-bind = "$attrs">
<use :xlink:href="iconName"></use>
</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 icon-${props.name}`
}
return 'svg-icon'
});
</script>
<style scoped lang ="scss">
.svg-icon{
width: 3em;
height: 3em;
fill: currentColor;
border: solid 2px red;
vertical-align: middle;
}
</style>>
d 全局注冊(cè) main.js
import { svgIcon } from './components'
.component('svg-icon',svgIcon)
e 創(chuàng)建導(dǎo)入處理函數(shù) 在plugins中創(chuàng)建 svgBulider.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) => {
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)
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>
`
)
}
}
}
f 修改配置文件,將svgBulider導(dǎo)入到配置文件
修改vite.config.js
import { svgBuilder } from './src/plugins/svgBuilder'; '
export default defineConfig({
plugins: [
vue(),
//調(diào)用對(duì)應(yīng)的函數(shù) 進(jìn)行 svg 處理
svgBuilder('./src/icons/')//對(duì)應(yīng)的文件夾,否則無(wú)法找到
]
})
g 使用SVG

核心部分
<svg-icon name="questionmark" />//name 取你的svg圖片
2. 使用fontAwesome
a npm 安裝依賴(lài)
$ npm i --save @fortawesome/fontawesome $ npm i --save @fortawesome/vue-fontawesome $ npm i --save @fortawesome/fontawesome-free-solid $ npm i --save @fortawesome/fontawesome-free-regular $ npm i --save @fortawesome/fontawesome-free-brands
b mian.js 全局注冊(cè)
//按需導(dǎo)入
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { library } from '@fortawesome/fontawesome-svg-core'
import { faAd } from '@fortawesome/free-solid-svg-icons'
import { faAddressBook } from '@fortawesome/free-solid-svg-icons'
library.add(faAddressBook)
// 全部導(dǎo)入
import { fas } from'@fortawesome/free-solid-svg-icons'
import { fab } from '@fortawesome/free-brands-svg-icons'
library.add(fas,fab)
.component('font-awesome-icon', FontAwesomeIcon)
c 使用
//按需導(dǎo)入的使用
<font-awesome-icon icon="address-book" class="fa-spin fa-lg"/>
//全局導(dǎo)入的使用
<font-awesome-icon :icon="['fas','address-book']" />

3 來(lái)源
來(lái)源 fontAwesome http://www.dhdzp.com/article/228944.htm
來(lái)源 svg http://www.dhdzp.com/article/228948.htm
4 總結(jié)
確定對(duì)應(yīng)的技術(shù)棧和版本,按照步驟,就沒(méi)什么問(wèn)題。
到此這篇關(guān)于Vue3使用icon的兩種方式的文章就介紹到這了,更多相關(guān)Vue3使用icon內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 二維碼長(zhǎng)按保存和復(fù)制內(nèi)容操作
這篇文章主要介紹了vue 二維碼長(zhǎng)按保存和復(fù)制內(nèi)容操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
vue3?TS?vite?element?ali-oss使用教程示例
這篇文章主要為大家介紹了vue3?TS?vite?element?ali-oss使用教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue?watch報(bào)錯(cuò):Error?in?callback?for?watcher?"xxx&qu
這篇文章主要給大家介紹了關(guān)于vue?watch報(bào)錯(cuò):Error?in?callback?for?watcher?“xxx“:“TypeError:Cannot?read?properties?of?undefined的解決方法,需要的朋友可以參考下2023-03-03
Vue3中動(dòng)態(tài)修改樣式與級(jí)聯(lián)樣式優(yōu)先順序圖文詳解
在項(xiàng)目中,我們時(shí)常會(huì)遇到動(dòng)態(tài)的去綁定操作切換不同的CSS樣式,下面這篇文章主要給大家介紹了關(guān)于Vue3中動(dòng)態(tài)修改樣式與級(jí)聯(lián)樣式優(yōu)先順序的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
Vue+Vite項(xiàng)目初建(axios+Unocss+iconify)的實(shí)現(xiàn)
一個(gè)好的項(xiàng)目開(kāi)始搭建總是需要配置許多初始化配置,本文就來(lái)介紹一下Vue+Vite項(xiàng)目初建(axios+Unocss+iconify)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Vue路由跳轉(zhuǎn)與接收參數(shù)的實(shí)現(xiàn)方式
這篇文章主要介紹了Vue路由跳轉(zhuǎn)與接收參數(shù)的實(shí)現(xiàn)方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
vue解析Json數(shù)據(jù)獲取Json里面的多個(gè)id問(wèn)題
這篇文章主要介紹了vue解析Json數(shù)據(jù)獲取Json里面的多個(gè)id問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
公共Hooks封裝文件下載useDownloadFile實(shí)例詳解
這篇文章主要為大家介紹了公共Hooks封裝文件下載useDownloadFile實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

