vite-plugin-svg-icons配置

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// vite.config.ts
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

export default defineConfig({
plugins: [
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
// 指定symbolId格式
symbolId: 'icon-[name]',
inject: 'body-last',
customDomId: '__svg__icons__dom__'
})
]
})

// main.ts
import 'virtual:svg-icons-register'

// SvgIcon.vue
<script lang="ts" setup>
import { computed } from 'vue'

defineOptions({
name: 'SvgIcon',
})

const props = withDefaults(defineProps<IProps>(), {
className: '',
color: '',
})

interface IProps {
iconClass: any
className?: string
color?: string
}

const iconName = computed(() => {
return `#icon-${props.iconClass}`
})

const svgClass = computed(() => {
if (props.className)
return `svg-icon ${props.className}`

return 'svg-icon'
})
</script>

<template>
<svg :class="svgClass" aria-hidden="true">
<use :xlink:href="iconName" :fill="color" />
</svg>
</template>

<style lang="scss" scoped>
.svg-icon {
width: 1em;
height: 1em;
position: relative;
fill: currentColor;
vertical-align: -2px;
}
</style>