11import 'ant-design-vue/dist/antd.less' ;
2- import { createApp , defineComponent , watch , ref } from 'vue' ;
2+ import { createApp , defineComponent , watch , ref , watchEffect , onMounted } from 'vue' ;
33import { createRouter , createWebHashHistory , useRoute , useRouter , RouteRecord } from 'vue-router' ;
44import { Avatar } from 'ant-design-vue' ;
55import { UserOutlined } from '@ant-design/icons-vue' ;
@@ -30,22 +30,36 @@ const BasicLayout = defineComponent({
3030 const menuData = getMenuData ( getRoutes ( ) ) ;
3131 globalState . menuData = menuData ;
3232
33- const cacheOpenKeys = ref < string [ ] > ( [ ] ) ;
34- watch (
35- ( ) => state . collapsed ,
36- ( collapsed : boolean ) => {
37- console . log ( 'post watch' , collapsed , state . collapsed ) ;
38- if ( collapsed ) {
39- cacheOpenKeys . value = state . openKeys ;
40- state . openKeys = [ ] ;
41- } else {
42- state . openKeys = cacheOpenKeys . value ;
43- }
44- } ,
45- {
46- flush : 'pre' ,
47- } ,
48- ) ;
33+ const updateSelectedMenu = ( ) => {
34+ const matched = route . matched . concat ( ) . map ( item => item . path ) ;
35+ matched . shift ( ) ;
36+ state . selectedKeys = matched ;
37+ } ;
38+
39+ onMounted ( ( ) => {
40+ // if sider collapsed, set openKeys is null.
41+ const cacheOpenKeys = ref < string [ ] > ( [ ] ) ;
42+ watch (
43+ ( ) => state . collapsed ,
44+ ( collapsed : boolean ) => {
45+ console . log ( 'post watch' , collapsed , state . collapsed ) ;
46+ if ( collapsed ) {
47+ cacheOpenKeys . value = state . openKeys ;
48+ state . openKeys = [ ] ;
49+ } else {
50+ state . openKeys = cacheOpenKeys . value ;
51+ }
52+ } ,
53+ {
54+ flush : 'pre' ,
55+ } ,
56+ ) ;
57+
58+ // watch route
59+ watchEffect ( ( ) => {
60+ updateSelectedMenu ( ) ;
61+ } ) ;
62+ } ) ;
4963
5064 return ( ) => (
5165 < RouteContextProvider value = { state } >
@@ -67,11 +81,7 @@ const BasicLayout = defineComponent({
6781 { ...{
6882 'onUpdate:collapsed' : $event => ( state . collapsed = $event ) ,
6983 'onUpdate:openKeys' : $event => ( state . openKeys = $event ) ,
70- 'onUpdate:selectedKeys' : ( ) => {
71- const matched = route . matched . concat ( ) . map ( item => item . path ) ;
72- matched . shift ( ) ;
73- state . selectedKeys = matched ;
74- } ,
84+ 'onUpdate:selectedKeys' : updateSelectedMenu ,
7585 } }
7686 v-slots = { {
7787 rightContentRender : ( ) => (
0 commit comments