|
@@ -1,22 +1,25 @@
|
|
|
<template>
|
|
|
<el-menu
|
|
|
+ ref="menuRef"
|
|
|
class="main-layout-left-menu"
|
|
|
unique-opened
|
|
|
:default-active="defaultActive"
|
|
|
- :default-openeds="defaultOpeneds"
|
|
|
+ :default-openeds="defaultOpened"
|
|
|
:collapse="mainLayoutStore.collapse"
|
|
|
+ @open="onOpen"
|
|
|
>
|
|
|
<menu-item
|
|
|
v-for="menu in mainLayoutStore.renderMenus"
|
|
|
:key="menu.index"
|
|
|
- :current-index="defaultActive"
|
|
|
+ :current-index="activeIndex"
|
|
|
:menu="menu"
|
|
|
+ @handle="onHandle"
|
|
|
></menu-item>
|
|
|
</el-menu>
|
|
|
</template>
|
|
|
|
|
|
<script setup lang="ts" name="MainLayoutLeftMenu">
|
|
|
-import { computed } from 'vue'
|
|
|
+import { computed, ref, watch } from 'vue'
|
|
|
import { useRoute } from 'vue-router'
|
|
|
import { ElMenu } from 'element-plus'
|
|
|
import useMainLayoutStore from '@/store/layout'
|
|
@@ -25,7 +28,9 @@ import MenuItem from '@/layout/main/MenuItem.vue'
|
|
|
const route = useRoute()
|
|
|
const mainLayoutStore = useMainLayoutStore()
|
|
|
|
|
|
-const defaultActive = computed(() => {
|
|
|
+const menuRef = ref<InstanceType<typeof ElMenu> & { close: (index: string) => void }>()
|
|
|
+
|
|
|
+const activeIndex = computed(() => {
|
|
|
if (route.meta?.menu) {
|
|
|
return route.meta.menuId
|
|
|
}
|
|
@@ -33,10 +38,28 @@ const defaultActive = computed(() => {
|
|
|
return ids?.slice(0, -1)?.join('-') || ''
|
|
|
})
|
|
|
|
|
|
-const defaultOpeneds = computed(() => {
|
|
|
- const rootSub = defaultActive.value.split('-')[0]
|
|
|
+const defaultActive = ref(activeIndex.value)
|
|
|
+
|
|
|
+const defaultOpened = computed(() => {
|
|
|
+ const rootSub = activeIndex.value.split('-')[0]
|
|
|
return [rootSub]
|
|
|
})
|
|
|
+
|
|
|
+watch(activeIndex, () => {
|
|
|
+ if (defaultActive.value) {
|
|
|
+ defaultActive.value = ''
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+let opened = ''
|
|
|
+
|
|
|
+const onOpen = (index: string) => {
|
|
|
+ opened = index
|
|
|
+}
|
|
|
+
|
|
|
+const onHandle = () => {
|
|
|
+ opened && menuRef.value?.close(opened)
|
|
|
+}
|
|
|
</script>
|
|
|
|
|
|
<style scoped lang="scss">
|