Vue3 + TS 使用 element-plus 的Icon组件(包括动态路由菜单图标)

1、安装包
# NPM
npm install @element-plus/icons-vue
# Yarn
yarn add @element-plus/icons-vue
2、全局注册
import { createApp } from 'vue';
// 引入 element-plus UI框架
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import * as ELIcons from '@element-plus/icons-vue';
import App from './App.vue';

const app = createApp(App);
for (const name in ELIcons) {
    app.component(name, (ELIcons as any)[name]);
}
app.use(ElementPlus).mount('#app');
3、在组件中使用
<el-icon :size="20">
 	<edit />
</el-icon>
// 按钮中使用
<el-button type="primary" :icon="Edit"></el-button>
4、动态路由菜单图标
// router 路由文件
export const constantRoutes: Array<RouteRecordRaw> = [
    {
        path: '/',
        component: layout,
        redirect: '/home',
        meta: {
            title: '首页',
            icon: 'home-filled',
        },
        children: [
            {
                path: '/home',
                name: '首页',
                component: () => import(/* webpackChunkName: "home" */ '@/views/home.vue'),
            },
        ],
    },
]
// 在用到 el-menu的文件中
<el-menu-item :key="item.path" :index="item.path" :route="item.path">
     <el-icon>
       <component :is="item.meta.icon" />
     </el-icon>
     <template #title>{{ item.meta.title }}</template>
</el-menu-item>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3和TypeScriptTS)结合使用Element Plus来进行角色菜单分配,是一种很好的选择。下面是一个关于如何实现角色菜单分配的简单示例。 首先,我们需要安装Vue 3、TypeScriptElement Plus。可以使用npm命令进行安装。 ```bash npm install vue@next npm install typescript npm install element-plus ``` 然后,在Vue 3项目的入口文件中,可以引入Element Plus的模块,以及一些额外的样式文件。 ```javascript import { createApp } from 'vue' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' import App from './App.vue' const app = createApp(App) app.use(ElementPlus) app.mount('#app') ``` 在组件中,我们可以使用Element Plus的Menu组件来展示菜单,并通过V-if指令来根据角色进行菜单项的展示与隐藏。 ```vue <template> <div> <el-menu> <el-menu-item v-if="hasAccess('dashboard')">仪表盘</el-menu-item> <el-menu-item v-if="hasAccess('users')">用户管理</el-menu-item> <el-menu-item v-if="hasAccess('roles')">角色管理</el-menu-item> </el-menu> </div> </template> <script> import { defineComponent } from 'vue' export default defineComponent({ data() { return { userRoles: ['dashboard', 'users', 'roles'] } }, methods: { hasAccess(role) { return this.userRoles.includes(role) } } }) </script> <style> /* 根据需要自定义样式 */ </style> ``` 在该示例中,假设用户角色以数组的形式存储在`userRoles`属性中。通过定义一个`hasAccess`方法,我们可以判断用户是否拥有某个角色,然后根据判断结果,在菜单项上使用V-if指令来展示或隐藏对应的菜单项。 当用户拥有某个角色时,该菜单项将会在界面上显示出来,反之,则不会显示。需要注意的是,需根据实际需求对示例中的角色判断逻辑进行调整。 这只是一个简单的示例,实际的角色菜单分配功能可能涉及到更复杂的逻辑和接口调用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值