使用svg图标

vue使用svg图标,

一、在src下assets新建icons,

二、icons新建svg文件夹,index.js文件,如图

三、svg文件夹存放自己下载的图标

四、index.js文件内容

import Vue from 'vue'

import SvgIcon from '../../components/SvgIcon'// svg component

// register globally

Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)

const requireAll = requireContext => requireContext.keys().map(requireContext)

requireAll(req)

五、在components新建SvgIcon ,如图

 六、SvgIcon   index.vue内容

<template>

  <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />

  <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">

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

  </svg>

</template>

<script>

import { isExternal } from '../../utils/validate'

export default {

  name: 'SvgIcon',

  props: {

    iconClass: {

      type: String,

      required: true

    },

    className: {

      type: String,

      default: ''

    }

  },

  computed: {

    isExternal() {

      return isExternal(this.iconClass)

    },

    iconName() {

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

    },

    svgClass() {

      if (this.className) {

        return 'svg-icon ' + this.className

      } else {

        return 'svg-icon'

      }

    },

    styleExternalIcon() {

      return {

        mask: `url(${this.iconClass}) no-repeat 50% 50%`,

        '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`

      }

    }

  }

}

</script>

<style scoped>

.svg-icon {

  width: 1em;

  height: 1em;

  vertical-align: -0.15em;

  fill: currentColor;

  overflow: hidden;

}

.svg-external-icon {

  background-color: currentColor;

  mask-size: cover!important;

  display: inline-block;

}

</style>

七、找到build文件夹下的webpack.base.conf.js里的module:{},找到 loader: 'url-loader',注释,或删除,因,你新添加的对象和创建项目时创建的有冲突,,如图

 八。添加两个对象,

 {

        test: /\.svg$/,

        loader: 'svg-sprite-loader',

        include: [resolve('src/assets/icons')],

        options: {

          symbolId: 'icon-[name]'

        }

      },

      {

        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,

        loader: 'url-loader',

        exclude: [resolve('src/assets/icons')],

        options: {

          limit: 10000,

          name: utils.assetsPath('img/[name].[hash:7].[ext]')

        }

      }

九、在main.js引入 

import './assets/icons/index' // icon

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值