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

在了路徑 src/assets 下新建文件夾 iconfont ,用來存放字體圖標(biāo)的本地文件
解壓下載到本地的字體圖標(biāo)文件,放到 iconfont 文件夾下
如過項目中沒有下載 css-loader 依賴包,就進(jìn)行下載,否則會報錯
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
// 引入并注冊全局組件
import iconUnicode from './ui/iconUnicode'
Vue.component('iUnicode', iconUnicode)
局部引入
// 局部引入并使用
import iSymbol from "../ui/iconSymbol"
import iFont from "../ui/iconFontClass"
export default {
//注冊
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)行封裝,但不管是在線使用還是本地使用,每次在項目中添加新圖標(biāo)之后都要更新一下 本地iconfont文件 或者 在線鏈接 。
demo 已上傳 GitHub
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?watch報錯:Error?in?callback?for?watcher?"xxx&qu
這篇文章主要給大家介紹了關(guān)于vue?watch報錯:Error?in?callback?for?watcher?“xxx“:“TypeError:Cannot?read?properties?of?undefined的解決方法,需要的朋友可以參考下2023-03-03
vue使用GraphVis開發(fā)無限拓展的關(guān)系圖譜的實現(xiàn)
本文主要介紹了vue使用GraphVis開發(fā)無限拓展的關(guān)系圖譜,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
詳解vue中v-model和v-bind綁定數(shù)據(jù)的異同
這篇文章主要介紹了vue中v-model和v-bind綁定數(shù)據(jù)的異同,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

