SVG 的优势是:
1、它是基于一个普遍应用的标准。
2、其是这种技术所形成的图像质量非常好。
3、其所形成的图像并不是很大,所以对图像进行保存和显示都十分的便捷,在这一过程中所选取的文件格式也具有非常强的灵活性,使用起来也非常的便捷。
4、其在运行的过程中可以起到很好的互动作用,同时还能够形成良好的动画效果
vue3+vite 项目中配置 SVG 图标:
一,安装 SVG 依赖插件
pnpm install vite-plugin-svg-icons
二,在 vite.config.ts 中配置插件
//引入SVG图标素材文件
createSvgIconsPlugin({
iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')
symbolId: 'icon-[dir]-[name]'
})
三,main.ts入口文件导入
import 'virtual:svg-icons-register'
四,svg 封装为全局组件
<template>
<svg aria-hidden="true">
<use :xlink:href="symbolId" />
</svg>
</template>
<script>
import { defineComponent, computed } from 'vue'
export default defineComponent({
name: 'SvgIcon',
props: {
prefix: {
type: String,
default: 'icon'
},
name: {
type: String,
required: true
}
},
setup(props) {
const symbolId = computed(() => `#${props.prefix}-${props.name}`)
return { symbolId }
}
})
</script>
五,在main.ts中注册全局组件
import svgIcon from '@/components/SvgIcon.vue'
app.component('svg-icon', svgIcon)
六,在业务组件中使用
<svg-icon name="user" class="icon-svg" />