master
2022-06-09 c6b04bbd291289e93131155249042c69c8d2d0bc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<template>
    <div class="cl-menu-topbar">
        <el-menu
            :default-active="index"
            mode="horizontal"
            background-color="transparent"
            @select="onSelect"
        >
            <el-menu-item v-for="(item, index) in list" :key="index" :index="`${index}`">
                <icon-svg v-if="item.icon" :name="item.icon" :size="18" />
                <span>{{ item.name }}</span>
            </el-menu-item>
        </el-menu>
    </div>
</template>
 
<script lang="ts">
import { computed, defineComponent, onMounted, ref } from "vue";
import { firstMenu } from "../../utils";
import { useCool } from "/@/cool";
 
export default defineComponent({
    name: "cl-menu-topbar",
 
    setup() {
        const { store, router, route } = useCool();
 
        // 选中标识
        const index = ref<string>("0");
 
        // 菜单列表
        const list = computed(() => store.getters.menuGroup.filter((e: any) => e.isShow));
 
        // 选择导航
        function onSelect(index: number) {
            store.commit("SET_MENU_LIST", index);
 
            // 获取第一个菜单地址
            const url = firstMenu(list.value[index].children);
            router.push(url);
        }
 
        onMounted(function () {
            // 设置默认
            function deep(e: any, i: number) {
                switch (e.type) {
                    case 0:
                        e.children.forEach((e: any) => {
                            deep(e, i);
                        });
                        break;
                    case 1:
                        if (route.path.includes(e.path)) {
                            index.value = String(i);
                            store.commit("SET_MENU_LIST", i);
                        }
                        break;
                    case 2:
                    default:
                        break;
                }
            }
 
            list.value.forEach((e: any, i: number) => {
                deep(e, i);
            });
        });
 
        return {
            index,
            list,
            onSelect
        };
    }
});
</script>
 
<style lang="scss">
.cl-menu-topbar {
    margin-right: 10px;
 
    .el-menu {
        height: 50px;
        background: transparent;
        overflow: hidden;
 
        .el-menu-item {
            display: flex;
            align-items: center;
            height: 50px;
            border-bottom: 0 !important;
            padding: 0 20px;
            background: transparent;
 
            span {
                font-size: 12px;
                margin-left: 3px;
                line-height: normal;
            }
 
            &.is-active {
                color: $color-primary;
            }
 
            .icon-svg {
                margin-right: 5px;
            }
        }
    }
}
</style>