vue中手動(dòng)封裝iconfont組件解析(三種引用方式的封裝和使用)
在線使用 有時(shí)候會(huì)因網(wǎng)絡(luò)問題影響用戶體驗(yàn);直接放在 本地使用 ,如果過多使用也會(huì)顯得繁瑣,所以就可以將其封裝成一個(gè)組件,也方便維護(hù)。?
封裝基于阿里巴巴圖標(biāo)庫的項(xiàng)目圖標(biāo)。
準(zhǔn)備
將項(xiàng)目?jī)?nèi)的圖標(biāo)下載至本地

在了路徑 src/assets 下新建文件夾 iconfont ,用來存放字體圖標(biāo)的本地文件
解壓下載到本地的字體圖標(biāo)文件,放到 iconfont 文件夾下
如過項(xiàng)目中沒有下載 css-loader 依賴包,就進(jìn)行下載,否則會(huì)報(bào)錯(cuò)
npm install css-loader -D
封裝
unicode引用封裝
<template>
<div>
<span class="iconfont" v-html="name"></span>
<slot></slot>
</div>
</template>
<script>
export default {
name: 'iconUnicode',
props: {
name: {
type: String,
required: true
}
}
}
</script>
<style scoped>
@font-face {
/* Unicode */
font-family: "iconfont";
src: url("../assets/iconfont/iconfont.eot");
src: url("../assets/iconfont/iconfont.eot?#iefix") format("embedded-opentype"),
url("../assets/iconfont/iconfont.woff2") format("woff2"),
url("../assets/iconfont/iconfont.woff") format("woff"),
url("../assets/iconfont/iconfont.ttf") format("truetype"),
url("../assets/iconfont/iconfont.svg#iconfont") format("svg");
}
.iconfont {
font-family: "iconfont" !important;
font-size: 2em;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
</style>font-class引用封裝
<template> ? <div> ? ? <span class="iconfont" :class="iconTag"></span> ? ? <slot></slot> ? </div> </template>
<script>
import "../assets/iconfont/iconfont.css";
export default {
? name: "iconFontClass",
? props: {
? ? name: {
? ? ? type: String,
? ? ? required: true
? ? }
? },
? computed: {
? ? iconTag() {
? ? ? return `icon-${this.name}`;
? ? }
? }
};
</script><style scoped>
.iconfont {
? font-family: "iconfont" !important;
? font-size: 2em;
? font-style: normal;
? -webkit-font-smoothing: antialiased;
? -moz-osx-font-smoothing: grayscale;
}
</style>symbol引用封裝
<template> ? <div> ? ? <svg class="icon" aria-hidden="true"> ? ? ? <use :xlink:href="iconTag" rel="external nofollow" ></use> ? ? </svg> ? ? <slot></slot> ? </div> </template>
<script>
import "../assets/iconfont/iconfont.js";
export default {
? name: "iconSymbol",
? props: {
? ? name: {
? ? ? type: String,
? ? ? required: true
? ? }
? },
? computed: {
? ? iconTag() {
? ? ? return `#icon-${this.name}`;
? ? }
? }
};
</script><style scoped>
.icon {
? width: 2em;
? height: 2em;
? vertical-align: -0.15em;
? fill: currentColor;
? overflow: hidden;
}
</style>引入
全局引入
// main.js
// 引入并注冊(cè)全局組件
import iconUnicode from './ui/iconUnicode'
Vue.component('iUnicode', iconUnicode)
局部引入
// 局部引入并使用
import iSymbol from "../ui/iconSymbol"
import iFont from "../ui/iconFontClass"
export default {
//注冊(cè)
components: {
iSymbol,
iFont
}
};
使用
<template>
<div class="box">
<i-symbol name="fanhuidingbu">Symbol</i-symbol>
<i-font name="fanhuidingbu">Font class</i-font>
<i-unicode name="" style="font-size:30px;color:#333">Unicode</i-unicode>
</div>
</template>
效果圖:

最后
也可以通過在線鏈接進(jìn)行封裝,但不管是在線使用還是本地使用,每次在項(xiàng)目中添加新圖標(biāo)之后都要更新一下 本地iconfont文件 或者 在線鏈接 。
demo 已上傳 GitHub
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3?setup的注意點(diǎn)及watch監(jiān)視屬性的六種情況分析
這篇文章主要介紹了Vue3?setup的注意點(diǎn)及watch監(jiān)視屬性的六種情況,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
Vue實(shí)現(xiàn)指令式動(dòng)態(tài)追加小球動(dòng)畫組件的步驟
這篇文章主要介紹了Vue實(shí)現(xiàn)指令式動(dòng)態(tài)追加小球動(dòng)畫組件的步驟,幫助大家更好的理解和實(shí)用vue,感興趣的朋友可以了解下2020-12-12
Vue中ElementUI結(jié)合transform使用時(shí)彈框定位不準(zhǔn)確問題解析
在近期開發(fā)中,需要將1920*1080放到更大像素大屏上演示,所以需要使用到transform來對(duì)頁面進(jìn)行縮放,但是此時(shí)發(fā)現(xiàn)彈框定位出錯(cuò)問題,無法準(zhǔn)備定位到實(shí)際位置,本文給大家分享Vue中ElementUI結(jié)合transform使用時(shí)彈框定位不準(zhǔn)確解決方法,感興趣的朋友一起看看吧2024-01-01
Vue開發(fā)實(shí)現(xiàn)滑動(dòng)驗(yàn)證組件
這篇文章主要為大家詳細(xì)介紹了如何利用Vue開發(fā)實(shí)現(xiàn)簡(jiǎn)單的滑動(dòng)驗(yàn)證組件,并且適配移動(dòng)和PC,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-07-07
vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能
這篇文章主要介紹了vue 使用微信jssdk,調(diào)用微信相冊(cè)上傳圖片功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11

