zhangxiao
2024-08-20 e47b788ff5f5c699c682999c95da17eb284ca21d
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
66
67
68
69
70
71
72
73
74
75
<template>
    <template v-if="checkVisible()">
        <a-sub-menu v-if="showSubMenu()" :key="props.route.path" v-bind="$attrs">
            <template #icon>
                <Svg v-if="route.meta && route.meta.icon" :name="route.meta.icon" :width="20" :height="20"></Svg>
            </template>
            <template #title>
                <span>{{ route.meta?.title && $t(route.meta?.title) }}</span>
            </template>
            <template v-for="child in route.children" :key="child.path">
                <menu-item :route="child" />
            </template>
        </a-sub-menu>
        <a-menu-item v-if="!showSubMenu()" :key="props.route.path" v-bind="$attrs" @click="onJump">
            <template #icon>
                <Svg v-if="route.meta && route.meta.icon" :name="route.meta.icon" :width="20" :height="20"></Svg>
            </template>
            <span>{{ route.meta?.title && $t(route.meta?.title) }}</span>
        </a-menu-item>
    </template>
</template>
<script setup lang="ts" name="MenuItem">
import { RouteConfig } from "types";
import { Svg } from "@easyfe/admin-component";
 
const router = useRouter();
 
const props = withDefaults(
    defineProps<{
        route: RouteConfig;
    }>(),
    {}
);
 
/** 获取是否存在子路由 */
const hasChild = computed(() => {
    if (!props.route.children?.length) {
        return false;
    }
    //如果子路由全部都是隐藏的,则认为没有子路由
    const hideRouters = props.route.children.filter(
        (item) => item.meta?.hidden === true || item.meta?.permission?.() === false
    );
    if (hideRouters.length === props.route.children.length) {
        return false;
    }
    return true;
});
 
function showSubMenu() {
    //如果没有子路由,不显示submenu
    if (!hasChild.value) {
        return false;
    }
    return true;
}
 
function checkVisible() {
    if (props.route.meta?.hidden) {
        return false;
    }
    if (props.route.meta?.permission?.() === false) {
        return false;
    }
    return true;
}
 
function onJump() {
    if (props.route.redirect) {
        router.push(props.route.redirect);
    } else {
        router.push(props.route);
    }
}
</script>