转载,原文。我记录一下,以后方便用。
准备工作
yanr add svg-sprite-loader -D
webpack配置
使用 webpack 的 exclude 和 include,让svg-sprite-loader只处理指定文件夹下的 svg,url-loaer只处理除此文件夹之外的 svg
1 | { |
新建组件
components/Svgicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41<template>
<svg :class="svgClass" aria-hidden="true">
<use :xlink:href="iconName"></use>
</svg>
</template>
<script>
export default {
props: {
iconClass: {
type: String,
required: true
},
className: {
type: String
}
},
computed: {
iconName() {
return `#icon-${this.iconClass}`
},
svgClass() {
if (this.className) {
return 'svg-icon ' + this.className
} else {
return 'svg-icon'
}
}
}
}
</script>
<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
fill: currentColor;
overflow: hidden;
}
</style>
存放icon svg
src创建icons文件夹
1 | // index.js |
使用方式
<svg-icon icon-class="svgname"></svg-icon>