左侧菜单栏中的图标名称其实是一个图标的组件,要自定义图标,就要将图标封装成一个组件,然后使用。
封装组件
src/components/svgIcon/index.vue
<template>
<div>
<img src="@/assets/tuceng.png" alt="" />
</div>
</template>
<script setup></script>
<style lang="less" scoped>
img {
width: 20px;
height: 20px;
}
</style>
全局引入注册
main.ts
import svgIcon from '@/components/svgIcon/index.vue';
app.component('svgIcon',svgIcon);
使用
router/index.ts
{
name: 'xxxx',
path: '/xxxx',
meta: { icon: 'svgIcon', title: '标题' },
}


文章介绍了如何在Vue项目中封装SVG图标为组件,首先展示了组件`src/components/svgIcon/index.vue`的代码,该组件使用`img`标签展示图标。然后在`main.ts`中全局引入并注册这个SVGIcon组件。最后,给出了在路由`router/index.ts`中如何使用这个自定义图标的例子。
2145

被折叠的 条评论
为什么被折叠?



