美文网首页
ElementUI使用第三方图标库Iconfont

ElementUI使用第三方图标库Iconfont

作者: mrpzx001 | 来源:发表于2019-10-17 14:49 被阅读0次

1. 在iconfont项目中生成在线链接

2. index.html中插入

```js

<script src="//at.alicdn.com/t/xxx.js"></script>

```

3. 新建一个组件Icon.vue

```vue

<template>

  <svg :class="svgClass" aria-hidden="true" :style="style">

    <use :xlink:href="iconName"></use>

  </svg>

</template>

<script>

export default {

  name: 'svg-icon',

  props: {

    name: {

      type: String,

      required: true

    },

    className: {

      type: String

    },

    size: {

      default: 20,

      type: [Number, String]

    },

    color: {

      type: String

    },

    rotate: {

      default: 0,

      type: Number

    }

  },

  computed: {

    iconName () {

      return `#icon-${this.name}`

    },

    svgClass () {

      if (this.className) {

        return 'svg-icon ' + this.className

      } else {

        return 'svg-icon'

      }

    },

    style () {

      let style = {}

      if (this.size) {

        style.fontSize = this.size + 'px'

      }

      if (this.color) {

        style.color = this.color

      }

      if (this.rotate) {

        style.transform = `rotate(${this.rotate}deg)`

      }

      return style

    }

  }

}

</script>

<style>

.svg-icon {

  width: 1em;

  height: 1em;

  vertical-align: -0.15em;

  fill: currentColor;

  overflow: hidden;

}

</style>

```

4. 引用组件即可

```js

import Icon from '@/components/Icon.vue'

```

在vue中使用:

<icon name="xxx" />

相关文章

网友评论

      本文标题:ElementUI使用第三方图标库Iconfont

      本文链接:https://www.haomeiwen.com/subject/qdolyctx.html