How do I use the SVG-Icon component in a new project?

1 Installation Dependencies

npm i svg-sprite-loader@4.13.
Copy the code

2 configuration vue. Config. Js

xxxxxxxxxx const path = require('path')function resolve (dir) {  return path.join(__dirname, dir)}

....

module.exports = {
  ...
  {
  // omit other...
  chainWebpack (config) {
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  }
}
}
Copy the code

3 Copy or create a file

(1) src/icons

SRC/ICONS - / SVG # Save ICONS - /index.js # Register global componentsCopy the code

在www.iconfont.cn/You can download the SVG you want to use

(2) components

componets/SvgIcon/index.vue
Copy the code

(3) validate.js

utils/validate.js

// The specific code (validate.js)
export function isExternal(path) {
  return /^(https? :|mailto:|tel:)/.test(path)
}
Copy the code

May also directly in the componets/SvgIcon/index of function without introducing the vue

4 Import in mian.js

import '@/icons'
Copy the code

5 Using Formats

'< svG-icon icon-class=" filename "/>Copy the code

The complete code

src/icons/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)
Copy the code

src/icons/svgo.yml

# replace default config

# multipass: true
# full: true

plugins:

  # - name
  #
  # or:
  # - name: false
  # - name: true
  #
  # or:
  # - name:
  #     param1: 1
  #     param2: 2

- removeAttrs:
    attrs:
      - 'fill'
      - 'fill-rule'

Copy the code

componets/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>
// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
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-The ${this.iconClass}`
    },
    svgClass() {
      if (this.className) {
        return 'svg-icon ' + this.className
      } else {
        return 'svg-icon'}},styleExternalIcon() {
      return {
        mask: `url(The ${this.iconClass}) no-repeat 50% 50%`.'-webkit-mask': `url(The ${this.iconClass}) no-repeat 50% 50%`}}}}</script>

<style scoped>
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15 em;
  fill: currentColor;
  overflow: hidden;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover! important;
  display: inline-block;
}
</style>

Copy the code