vue aside从router中获取label和name

<template>
    <el-menu :default-active="defaultPath" :collapse="props.isCollapse" router>
        <template v-for="(item1, index1) in menus" :key="index1">
            <!-- 一级菜单 -->
            <el-sub-menu :index="item1.path" v-if="(item1.children || []).length">
                <template #title>
                    <el-icon>
                        <component :is="item1.icon"></component>
                    </el-icon>
                    <span>{{item1.title}}</span>
                </template>
                <template v-for="(item2, index2) in (item1.children || [])" :key="index2">
                    <!-- 二级菜单 -->
                    <el-menu-item :index="item2.path">{{item2.title}}</el-menu-item>
                </template>
            </el-sub-menu>
            <!-- 一级菜单 -->
            <el-menu-item :index="item1.path" v-else>
                <el-icon>
                    <component :is="item1.icon"></component>
                </el-icon>
                <template #title>{{item1.title}}</template>
            </el-menu-item>
        </template>
    </el-menu>
</template>

<script setup>
    import { defineProps, computed } from 'vue'
    import { useStore } from 'vuex'
    import { useRoute } from 'vue-router'
    import { routes } from '@/router/routes'

    const { getters } = useStore()

    const hasPermission = computed(() => getters['login/hasPermission'])

    const props = defineProps({
        isCollapse: {
            type: Boolean,
            required: true
        }
    })

    const getMenus = (base = '/', list = []) => {
        return list.
            filter(i => (i.meta || {}).isMenu === true).
            map(i => {
                if (i.meta && i.meta.permissions && !hasPermission.value(...i.meta.permissions)) {
                    return {}
                }
                let obj = {
                    path: base + i.path,
                    title: i.meta.title,
                    icon: i.meta.icon || 'Operation'
                }
                if (i.children && i.children.length) {
                    let children = getMenus(base + i.path + '/', i.children)
                    if (!children.length) {
                        return {}
                    }
                    obj.children = children
                }
                return obj
            }).filter(i => i.path !== undefined)
    }
    const menus = getMenus((routes[0] || {}).path, (routes[0] || {}).children || [])


    const route = useRoute()
    const defaultPath = route.path ? route.path : ((menus[0] || {}).path || '')
</script>

<style lang="scss" scoped>
    .el-menu {
        border-right: none;
    }
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值