1- import { defineComponent , watch , computed , onMounted } from 'vue'
2- import { useRoute } from 'vue-router'
1+ import { defineComponent , watch , Ref , ref , onMounted } from 'vue'
2+ import { useRoute , useRouter , RouteRecordRaw } from 'vue-router'
33import { Breadcrumb } from 'ant-design-vue'
44
55import styles from './index.module.less'
@@ -8,23 +8,64 @@ const ABreadCrumb = defineComponent({
88 name : 'ABreadCrumb' ,
99 setup ( { props } ) {
1010 const route = useRoute ( )
11+ const router = useRouter ( )
12+
13+ const breadList : Ref < string [ ] > = ref ( [ ] )
1114
1215 onMounted ( ( ) => {
13- console . log ( breadList )
16+ getRouteBreadList ( )
1417 } )
1518
1619 // 监听路由变化
17- const breadList = computed ( ( ) => {
18- const paths = route . path . split ( '/' )
20+ watch ( route , ( val ) => {
21+ breadList [ 'value' ] = [ ]
22+ getRouteBreadList ( )
1923 } )
2024
25+ // 获取路由地址列表
26+ const getRouteBreadList = ( ) => {
27+ const paths = route . path . split ( '/' )
28+ if ( paths . length > 1 ) {
29+ const menus = getMenus ( ) . filter (
30+ ( item : RouteRecordRaw ) => item . name == paths [ 1 ] ,
31+ )
32+ if ( menus . length ) {
33+ getRouteDict ( menus , paths )
34+ }
35+ }
36+ }
37+
38+ // 根据路由获取面包屑列表
39+ const getRouteDict = ( menus : any , paths : string [ ] ) => {
40+ menus . forEach ( ( item : any ) => {
41+ if ( paths . includes ( item . name ) ) {
42+ breadList [ 'value' ] . push ( item ?. meta ?. title )
43+ }
44+ if ( item . children && item . children . length ) {
45+ getRouteDict ( item . children , paths )
46+ }
47+ } )
48+ }
49+
50+ // 获取路由列表
51+ const getMenus = ( ) => {
52+ let menuList : RouteRecordRaw [ ] = [ ]
53+ const routes : Array < RouteRecordRaw > = router . options ?. routes || [ ]
54+ routes . forEach ( ( item ) => {
55+ if ( item . path == '/' ) {
56+ menuList = item . children || [ ]
57+ }
58+ } )
59+ return JSON . parse ( JSON . stringify ( menuList ) )
60+ }
61+
2162 return ( ) => (
2263 < div class = { styles [ 'bread-crumb' ] } >
2364 < span class = { styles [ 'location' ] } > 当前位置:</ span >
2465 < Breadcrumb >
25- < Breadcrumb . Item > Home </ Breadcrumb . Item >
26- < Breadcrumb . Item > Application </ Breadcrumb . Item >
27- < Breadcrumb . Item > Center </ Breadcrumb . Item >
66+ { breadList [ 'value' ] . map ( ( item ) => {
67+ return < Breadcrumb . Item > { item } </ Breadcrumb . Item >
68+ } ) }
2869 </ Breadcrumb >
2970 </ div >
3071 )
0 commit comments