vue中引入.svg图标,使用iconfont图标库

阿里巴巴的iconfont是一个很好的图标库,海量的素材可以快速满足开发人员日常对图标的诉求,我们采用symbol引用,官方介绍

创建SvgIcon组件
–components =>SvgIcon=>index.vue

<template>
  <svg :class="svgClass" aria-hidden="true">
    <use :xlink:href="iconName"></use>
  </svg>
</template>
 
<script>
export default {
  name: 'svg-icon',
  props: {
    iconClass: {
      type: String,
      required: true
    },
    className: {
      type: String
    }
  },
  computed: {
    iconName() {
      return `#icon-${this.iconClass}`
    },
    svgClass() {
      if (this.className) {
        return 'svg-icon ' + this.className
      } else {
        return 'svg-icon'
      }
    }
  }
}
</script>
 
<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>

创建icons文件夹
src跟目录下创建icons文件夹,里面创建svg文件夹和index.js文件在这里插入图片描述

svg文件夹中用来存放各种扩展的.svg图标。
在这里插入图片描述

index.js

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg组件
 
// register globally
Vue.component('svg-icon', SvgIcon)
 
const requireAll = requireContext => requireContext.keys().map(requireContext)
const req = require.context('./svg', false, /\.svg$/)
requireAll(req)

main.js中引入

import './icons'

下载插件

cnpm i svg-sprite-loader --save

配置
在build/webpack.base.conf.js文件中,加入

  {
    test: /\.svg$/,
    loader: 'svg-sprite-loader',
    include: [resolve('src/icons')],
    options: {
      symbolId: 'icon-[name]'
    }
  }

并在以下设置中添加exclude: [resolve(‘src/icons’)],如下所示

  {
    test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
    loader: 'url-loader',
    exclude: [resolve('src/icons')],
    options: {
      limit: 10000,
      name: utils.assetsPath('img/[name].[hash:7].[ext]')
    }
  },

使用
ok,大功告成,这样就可以在.vue页面中直接使用.svg文件了。使用.svg文件的名称,通过icon-class属性来引用。

<svg-icon icon-class="user" />

后续如果继续需要使用其他的图标直接去阿里巴巴官网下面就好啦!!

这里点击下载
在这里插入图片描述
在这里插入图片描述
然后将文件丢到icon文件夹下面
在这里插入图片描述
然后使用

   <svg-icon icon-class="share" />

复制的svg图标

<svg t="1619747190551" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="919" width="200" height="200">

<path d="M838 527.5c-29 0-52.6 23.5-52.6 52.6v223.6c0 25.1-20.5 45.6-45.6 45.6H215.2c-25.1 0-45.6-20.5-45.6-45.6V279c0-25.1 20.5-45.6 45.6-45.6h219.9c29 0 52.6-23.5 52.6-52.6s-23.5-52.6-52.6-52.6H215.2c-83.1 0-150.7 67.6-150.7 150.7v524.7c0 83.1 67.6 150.7 150.7 150.7h524.6c83.1 0 150.7-67.6 150.7-150.7V580.1c0-29-23.5-52.6-52.5-52.6z" p-id="920"></path>
<path d="M907 69.6H623.5c-29 0-52.6 23.5-52.6 52.6s23.5 52.6 52.6 52.6h163.4L449.2 512.3c-20.5 20.5-20.5 53.8 0 74.3 10.3 10.3 23.7 15.4 37.2 15.4 13.4 0 26.9-5.1 37.2-15.4l330.9-330.9v157.7c0 29 23.5 52.6 52.6 52.6s52.6-23.5 52.6-52.6V122.2c-0.1-29.1-23.7-52.6-52.7-52.6z" p-id="921"></path>
</svg>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值