关于vue-cli 2.x版本的svg-icon引用

203 阅读1分钟

学习原因

最近在研究ruoyi的后台权限管理系统时,发现了svg-icon的使用,相比iconfont来说,貌似都是本地引入后使用标签,个人还是习惯iconfont。既然使用到了,就研究一下怎么使用。这边记录一下适用于vue-cli 2.x版本的引用方式,后边附加3.x的配置

  • 安装svg-sprite-loader: npm install svg-sprite-loader --save
  • 在src/component文件夹下新建一个SvgIcon的组件
  • 组件的index.js内容
<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>

  • 前边漏了一个方法isExternal
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}
  • src/assets下新建文件icons,下级文件是svg文件(存放svg文件)和index.js,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)

  • 这边是2.x的配置方式:在build的webpack.base.conf.js文件中,找到module的rules,添加以下规则
      {
        test: /\.svg$/,
        loader: 'svg-sprite-loader',
        include: [resolve('src/assets/icons')],
        options: {
          symbolId: 'icon-[name]'//去掉svg这个图片加载不出来
        }
      },
      {//这条规则无需新增,修改exclude的内容即可
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: "url-loader",
        //这个不处理svg图片,因为我们独立拆开让svg-sprite-loader来处理了
        exclude: [resolve('src/assets/icons')],
        options: {
          limit: 10000,
          name: utils.assetsPath("img/[name].[hash:7].[ext]")
        }
      },
  • 这边是3.x的配置方式,需要到vue.config.js中配置,module.exports添加chainWebpack
chainWebpack(config) {
        // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
}
  • 组件中使用
<svg-icon icon-class="404" />
//添加类名
<svg-icon class-name="search-icon" icon-class="search"/>