|
目录
1、安装依赖
2、组件全局注册
2.1、使用完整的vue.js
2.2 main.js文件到导入注册组件
2.3、icons的 index.js 文件
3、Svg组件
3.1、Svg组件文件
3.2、校验工具类
4、Svg配置
4.1、在 vue.config.js 中,新增svg配置
4.2、svg文件夹
5、使用
1、安装依赖
cnpm install svg-sprite-loader --save-dev
2、组件全局注册
2.1、使用完整的vue.js
注意:
在脚手架中,我们使用的 import Vue from 'vue' 这里的vue 默认使用的是vue.runtime.commmon.js, 这个文件可以理解为不完整的,我们在全局组件注册时会有问题, 因此我们需要使用完整的 vue.js

在 vue.config.js 中, 使用 'vue': 'vue/dist/vue.js' 指定完整的 vue.js
'use strict'
const path = require('path')
function resolve(dir) {
return path.join(__dirname, dir)
}
module.exports = {
devServer: {
// 自动打开浏览器
open: true,
port: 8808
},
configureWebpack: (config) => {
config.resolve = {
extensions: ['.js', '.json', '.vue'],
alias: {
'@': path.resolve(__dirname, './src'),
// 使用完整的vue.js
'vue': 'vue/dist/vue.js'
}
}
}
}
2.2 main.js文件到导入注册组件
这里 import 的文件也就是 src / assets / icons / index.js 文件
import './assets/icons' // icon
2.3、icons的 index.js 文件
新建 src / assets / icons / index.js 文件,因为我们在步骤 2.1 已经把 vue.js 改成完整的了, 所以能使用全局注册组件
Vue.component('svg-icon', SvgIcon)
import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component
// register globally
Vue.component('svg-icon', SvgIcon)
/**
* require.context: 读取指定目录的所有文件
* 第一个参数: 目录
* 第二个参数: 是否遍历子级目录
* 第三个参数: 定义遍历文件规则, /\.svg$/ 是表示已 .svg结尾的文件
*/
const req = require.context('./svg', false, /\.svg$/)
const requireAll = (requireContext) => {
return requireContext.keys().map(requireContext)
}
requireAll(req)
3、Svg组件
3.1、Svg组件文件
新建 src / 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 :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() {
console.log(`#icon-${this.iconClass}`)
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>
3.2、校验工具类
在步骤 3.1 中使用到了isExternal 对象(在校验工具类中)
创建 src / utils / validate.js 文件
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
4、Svg配置
4.1、在 vue.config.js 中,新增svg配置
module.exports = {
// ........
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()
}
}
4.2、svg文件夹
新建 src / assets / icons / svg 文件夹, 用于存放项目中用到的 svg 文件, 并将 svg 文件存放在里面,
svg文件见附件
5、使用
随便在 .vue ,拿 src / layout / components / AppMain.vue 举例
<template>
<section class="app-main">
app-main
<svg-icon icon-class='system'/>
</section>
</template>
效果:

|