1- import { defineComponent , ref , watch , computed } from 'vue'
1+ import { defineComponent , ref , watch , computed , onMounted } from 'vue'
22import { useRoute , useRouter , RouteRecordRaw } from 'vue-router'
33import { Menu } from 'ant-design-vue'
44
@@ -19,7 +19,12 @@ const Menus = defineComponent({
1919 setup ( props , { slots } ) {
2020 const route = useRoute ( )
2121 const router = useRouter ( )
22- const activeRoute = ref ( route . path ) // 当前路由
22+ const activeRoute = ref ( ) // 当前路由
23+
24+ onMounted ( ( ) => {
25+ const paths = route . path . split ( '/' )
26+ activeRoute . value = paths [ paths . length - 1 ]
27+ } )
2328
2429 // 默认展开项
2530 const openKeys = computed ( ( ) => {
@@ -36,13 +41,14 @@ const Menus = defineComponent({
3641
3742 // 监听路由变化
3843 watch ( route , ( val ) => {
39- activeRoute . value = val . path
44+ const paths = val . path . split ( '/' )
45+ activeRoute . value = paths [ paths . length - 1 ]
4046 } )
4147
4248 // 子级导航渲染
4349 const SubItem = ( menus : any ) => {
4450 return (
45- < Menu . SubMenu key = { menus . path } >
51+ < Menu . SubMenu key = { menus . name } >
4652 { {
4753 title : ( ) => (
4854 < >
@@ -57,7 +63,7 @@ const Menus = defineComponent({
5763 { menus . children . map ( ( menu : any ) => {
5864 if ( ! menu . children || ! menu . children . length ) {
5965 return (
60- < Menu . Item key = { menu . path } >
66+ < Menu . Item key = { menu . name } >
6167 { menus . meta ?. icon && (
6268 < span
6369 class = { `iconfont ${ menu . meta . icon } menu-icon` }
@@ -92,7 +98,7 @@ const Menus = defineComponent({
9298 { ( props . menuLists as RouteRecordRaw [ ] ) . map ( ( menu ) => {
9399 if ( ! menu . children || ! menu . children . length ) {
94100 return (
95- < Menu . Item key = { menu . path } >
101+ < Menu . Item key = { menu . name } >
96102 { menu . meta ?. icon && (
97103 < span class = { `iconfont ${ menu . meta . icon } menu-icon` } > </ span >
98104 ) }
0 commit comments