fix(menu): 刷新后菜单选中项不匹配的问题

This commit is contained in:
萌狼蓝天 2025-03-13 17:39:19 +08:00
parent 54966747c6
commit 0044cdb62c

View File

@ -12,15 +12,16 @@
</div>
</template>
<script setup>
import { h, ref } from 'vue';
import {h, onMounted, ref, watch} from 'vue';
import {
MailOutlined,
CalendarOutlined,
AppstoreOutlined,
SettingOutlined,
} from '@ant-design/icons-vue';
import {useRouter} from "vue-router";
import {useRoute, useRouter} from "vue-router";
const router = useRouter();
const route = useRoute(); //
const theme = ref('light');
const selectedKeys = ref(['1']);
const openKeys = ref(['sub1']);
@ -68,4 +69,36 @@ const handleMenuClick = ({ key }) => {
router.push(item.path);
}
};
// key
const findSelectedKey = (menuItems, currentPath) => {
for (const item of menuItems) {
//
if (item.path === currentPath) {
return [item.key];
}
//
if (item.children) {
const found = findSelectedKey(item.children, currentPath);
if (found) return found;
}
}
return null;
};
//
const updateMenuState = () => {
const matchedKeys = findSelectedKey(items.value, route.path);
if (matchedKeys) {
selectedKeys.value = matchedKeys;
}
};
//
watch(() => route.path, () => {
updateMenuState();
});
//
onMounted(updateMenuState);
</script>