在vue中引入Symbol形式的iconfont

  1. 在 iconfont 官網中選擇並下載需要使用的圖標
  2. 在 static 文件夾中添加iconfont.js文件
  3. index.html文件中引入iconfont.js文件,需注意JS文件的引入路徑,如<script type="text/javascript" src="static/iconfont.js"></script>
  4. 創建 icon-components 組件
<template>
  <svg class="svg-icon" aria-hidden="true">
    <use :xlink:href="iconName"></use>
  </svg>
</template>

<script>
export default {
  name: 'icon-svg',
  props: {
    iconClass: {
      type: String,
      required: true
    }
  },
  computed: {
    iconName () {
      return `#icon-${this.iconClass}`
    }
  }
}
</script>

<style>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>
  1. main.js中引入以下圖標
    在這裏插入圖片描述
//引入svg組件
import IconSvg from './components/Detail/icon-components.vue'
//全局註冊icon-svg
Vue.component('icon-svg', IconSvg)
  1. 在代碼中使用
<icon-svg icon-class="pengyouquan" />
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章