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.dbjr.com.cn/article/228944.htm
來(lái)源 svg http://www.dbjr.com.cn/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項(xiàng)目如何引入json數(shù)據(jù)
這篇文章主要介紹了vue項(xiàng)目如何引入json數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue使用el-table實(shí)現(xiàn)表格跨頁(yè)多選
在我們?nèi)粘m?xiàng)目開(kāi)發(fā)中,經(jīng)常會(huì)有表格跨頁(yè)多選的需求,接下來(lái)讓我們用?el-table示例一步步來(lái)實(shí)現(xiàn)這個(gè)需求,文中有詳細(xì)的代碼講解,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08vue.js獲取數(shù)據(jù)庫(kù)數(shù)據(jù)實(shí)例代碼
本篇文章主要介紹了vue.js獲取數(shù)據(jù)庫(kù)數(shù)據(jù)實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05vue項(xiàng)目keepAlive配合vuex動(dòng)態(tài)設(shè)置路由緩存方式
vue項(xiàng)目keepAlive配合vuex動(dòng)態(tài)設(shè)置路由緩存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)
這篇文章主要介紹了Vue完整項(xiàng)目構(gòu)建(進(jìn)階篇)的相關(guān)資料,需要的朋友可以參考下2018-02-02Vue引入高德地圖并觸發(fā)實(shí)現(xiàn)多個(gè)標(biāo)點(diǎn)的示例詳解
這篇文章主要介紹了Vue引入高德地圖并觸發(fā)實(shí)現(xiàn)多個(gè)標(biāo)點(diǎn),主要是在public下的index.html中引入地圖,引入組件設(shè)置寬高100%,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05vue項(xiàng)目使用electron進(jìn)行打包操作的全過(guò)程
我們都知道Electron項(xiàng)目分為了主進(jìn)程和渲染進(jìn)程,主進(jìn)程其實(shí)就是我們的Electron,渲染進(jìn)程就相當(dāng)于我們的Vue項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目使用electron進(jìn)行打包操作的全過(guò)程,需要的朋友可以參考下2023-03-03websocket+Vuex實(shí)現(xiàn)一個(gè)實(shí)時(shí)聊天軟件
這篇文章主要利用websocked 建立長(zhǎng)連接,利用Vuex全局通信的特性,以及watch,computed函數(shù)監(jiān)聽(tīng)消息變化,并驅(qū)動(dòng)頁(yè)面變化實(shí)現(xiàn)實(shí)時(shí)聊天,感興趣的可以了解一下2021-08-08