盒子
盒子
文章目录
  1. 插件引入
  2. 插件配置
  3. 组件引用
  4. svg加载
  5. svg 使用

SVG

插件引入

npm install svg-sprite-loader --save-dev

插件配置

// vue.config.js
'use strict';
const path = require('path');

function resolve(dir) {
return path.join(__dirname, dir)
}

module.exports = {
publicPath: './',
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: false,
// ...
chainWebpack(config) {
// set svg-sprite-loader,配置svg路径
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();
}
// ...
};

组件引用

// components/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 :href="iconName" />
</svg>
</template>

<script>
// doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage

export default {
name: 'SvgIcon',
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String,
default: ''
}
},
computed: {
isExternal() {
return /^(https?:|mailto:|tel:)/.test(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: 1.2em;
height: 1.2em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}

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

svg加载

// src/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);

svg 使用

// 创建svg文件到 src/icons/svg
<svg-icon icon-class="password" class-name="myclass" />
支持一下
扫一扫,支持一下
  • 微信扫一扫
  • 支付宝扫一扫