Key points:

  • Svg-icon is a global component insrc/icons/index.jsGlobal components are defined and registered in.
  • Introduced in main.js
  • It requires additional coordination with [email protected] and the corresponding configuration in vue.config.js.
  • Its use format< svG-icon icon-class=" filename "/>. The file name in this case is the file name under SRC/ICONS/SVG

Steps:

1. Install the dependency package NPM I [email protected]

2. Configure the vue. Config. Js

Const path = require('path') function resolve (dir) {return path.join(__dirname, dir)}  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/create files:

1).src/icons

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

/ SVG to save icon files

The contents of the/index. Js

         
   import Vue from 'vue'
   import SvgIcon from '@/components/SvgIcon'
    
   Vue.component('svg-icon', SvgIcon)

   const req = require.context('./svg', false, /\.svg$/)
   const requireAll = requireContext => requireContext.keys().map(requireContext)
   requireAll(req)      
Copy the code

2) component:

  src/componets/SvgIcon/index.vue
Copy the code
<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-${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.15 em. fill: currentColor; overflow: hidden; } .svg-external-icon { background-color: currentColor; mask-size: cover! important; display: inline-block; } </style>Copy the code

3)validate.js

  src/utils/validate.js
Copy the code
/** * Created by PanJiaChen on 16/11/18. */ /** * @param {string} path * @returns {Boolean} */ export function isExternal(path) { return /^(https? :|mailto:|tel:)/.test(path) } /** * @param {string} str * @returns {Boolean} */ export function validmobile(str) { return /^1[3-9]\d{9}$/.test(str) }Copy the code

4. Import import ‘@/ ICONS ‘in main.js

5. Use:

Use the format < svG-icon icon-class=” file name “/>. The file name in this case is the file name under SRC/ICONS/SVG

3. Alibaba vector icon address

Select the image dot star dot favorites to copy the SVG code

www.iconfont.cn/collections…