广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >vue中手动封装iconfont组件解析(三种引用方式的封装和使用)
  • 865
分享到

vue中手动封装iconfont组件解析(三种引用方式的封装和使用)

2024-04-02 19:04:59 865人浏览 八月长安
摘要

目录准备封装unicode引用封装font-class引用封装symbol引用封装引入全局引入局部引入使用在线使用 有时候会因网络问题影响用户体验;直接放在 本地使用 ,如果过多使用

在线使用 有时候会因网络问题影响用户体验;直接放在 本地使用 ,如果过多使用也会显得繁琐,所以就可以将其封装成一个组件,也方便维护。​

封装基于阿里巴巴图标库的项目图标。

准备

将项目内的图标下载至本地

img

在了路径 src/assets 下新建文件夹 iconfont ,用来存放字体图标的本地文件

解压下载到本地的字体图标文件,放到 iconfont 文件夹下

如过项目中没有下载 CSS-loader 依赖包,就进行下载,否则会报错

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 {
  
  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="&#xe633;" style="font-size:30px;color:#333">Unicode</i-unicode>
  </div>
</template>

效果图:

最后

也可以通过在线链接进行封装,但不管是在线使用还是本地使用,每次在项目中添加新图标之后都要更新一下 本地iconfont文件 或者 在线链接 。

demo 已上传 GitHub

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。 

--结束END--

本文标题: vue中手动封装iconfont组件解析(三种引用方式的封装和使用)

本文链接: https://www.lsjlt.com/news/167132.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作