修复bug
This commit is contained in:
@@ -45,7 +45,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ref, computed, watch, withDefaults } from 'vue'
|
||||
import { ref, computed, watch, withDefaults, onMounted } from 'vue'
|
||||
import type { MenuGroup, MenuChild } from '../types.uts'
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
@@ -269,34 +269,44 @@ const handleClick = (c: MenuChild) => {
|
||||
emit('sub-click', c)
|
||||
}
|
||||
|
||||
/** 自动:groups 变更/activeSubId 无效时,默认跳第一个 leaf 并展开对应 group */
|
||||
/** 自动:groups 变更/activeSubId 无效时,只做状态同步(展开对应 group),不触发导航 */
|
||||
const ensureDefault = () => {
|
||||
if (!props.groups || props.groups.length === 0) return
|
||||
|
||||
const hit = findChildById(props.activeSubId)
|
||||
if (hit) {
|
||||
openGroupKey.value = findGroupKeyByChildId(hit.id)
|
||||
return
|
||||
}
|
||||
if (hit) { openGroupKey.value = findGroupKeyByChildId(hit.id); return }
|
||||
|
||||
// ✅ 再按当前 route 找一次
|
||||
try {
|
||||
const pages = getCurrentPages()
|
||||
// @ts-ignore
|
||||
const cur: any = pages[pages.length - 1]
|
||||
const route: string = cur?.route ?? ''
|
||||
const byRoute = findChildByPath(route)
|
||||
if (byRoute) { openGroupKey.value = findGroupKeyByChildId(byRoute.id); return }
|
||||
} catch(e) {}
|
||||
|
||||
const first = firstLeaf()
|
||||
if (first) {
|
||||
openGroupKey.value = findGroupKeyByChildId(first.id)
|
||||
emit('sub-click', first)
|
||||
}
|
||||
if (first) openGroupKey.value = findGroupKeyByChildId(first.id)
|
||||
}
|
||||
|
||||
// ✅ 移除 watch(immediate: true) 中的自动 emit,仅在 groups/activeSubId 变更时同步状态
|
||||
watch(
|
||||
() => props.groups,
|
||||
() => { ensureDefault() },
|
||||
{ immediate: true, deep: true }
|
||||
{ immediate: false, deep: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
() => props.activeSubId,
|
||||
() => { ensureDefault() },
|
||||
{ immediate: true }
|
||||
{ immediate: false }
|
||||
)
|
||||
|
||||
// ✅ 初始化时只做一次状态同步(不通过 watch immediate)
|
||||
onMounted(() => {
|
||||
ensureDefault()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style>
|
||||
|
||||
0
layouts/admin/components/AdminTopBar.uvue
Normal file
0
layouts/admin/components/AdminTopBar.uvue
Normal file
Reference in New Issue
Block a user