跳至主要內容

自定义symbol图标

wangdx大约 1 分钟

图标

阿里图标库open in new window

symbol 雪碧图

1.
//at.alicdn.com/t/c/font_4494682_yjke7ot7s6b.js
<script src="http://at.alicdn.com/t/c/font_4494682_yjke7ot7s6b.js"></script>

2.
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentcolor;
  overflow: hidden;
}
3.
<template>
  <div id="app">
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-a-houzikatongdongwu" />
    </svg>
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-a-hudiekunchongkatong" />
    </svg>
    <svg class="icon" aria-hidden="true">
      <use xlink:href="#icon-a-jikatongdongwu" />
    </svg>
  </div>
</template>

svg-sprite-loader

1.
pnpm i -D svg-sprite-loader
2.
 vue inspect --rules
 vue inspect --rules svg
3. main.js
import './icons/a-houzikatongdongwu.svg'
import './icons/a-hudiekunchongkatong.svg'
import './icons/a-jikatongdongwu.svg'
4. vue.config.js
const path = require('path')
module.exports = defineConfig({
 ....
  chainWebpack(config) {

    // set svg-sprite-loader
    config.module.rule('svg').exclude.add(resolve('src/icons')).end()
    config.module
      .rule('iconSvg')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
	......
  }
})

1. icons/svg icons/index.js
//目录,是否递归,规则
const req = require.context('./svg', false, /\.svg$/)
req.keys().forEach(req)
2.
import './icons'

自定义组件

1. SvgIcon/index.vue
<template>
  <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners"></div>
  <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>

2.
/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}
3. 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$/)
req.keys().forEach(req)

<svg-icon icon-class="a-houzikatongdongwu" class="el-input__icon input-icon" />
<svg-icon icon-class="a-hudiekunchongkatong" class="el-input__icon input-icon" />
<svg-icon icon-class="a-jikatongdongwu" class="el-input__icon input-icon" />
上次编辑于: