vue面包屑(vue动态路由多级嵌套面包屑怎么弄)

vue动态路由多级嵌套面包屑怎么弄(不是动态路由嵌套可以尝试用 this.$route.matched方法获取到path和name集合,动态的嵌套获取不到全部具体的id)

功能比如:A列表页面路由如/a,点击任意一列进入任意一个A的详情页面名字为B,/b/03(这个是动态路由弄是吧,03就是id嘛),点击B页面任意一列,再进入B的详情页名字为C,路由如/bdetail/01;现在弄面包屑要获取到的路由是刚刚打开的,如(/a;/b/03;/bdetail/01)

思路:获取所有进入的层级的路由和名称如breadlist=[{path:'/a',name:'一级'},{path:'/b/03',name:'二级'},{path:'/bdetail/01',name:'三级'}],然后遍历出来如:{{item.name}}

做法:下面贴出相关代码:

A列表页面跳转按钮:(breadNum记录面包屑层级)


B列表页面跳转按钮:


breadcrumb.vue页面:


状态管理store.js代码:

export default store = new Vuex.Store({
  state: {
    breadListState:[
      {name:'首页',path:'/'}
    ]
  },
  mutations: {
    breadListStateAdd(state,obj){
      state.breadListState.push(obj);
    },
    breadListStateRemove(state,num){
      state.breadListState=state.breadListState.slice(0,num);
    }
  }

})

路由route.js代码:

{
    path: '/',
    name: '首页',
    component: Main,
    redirect:'/home',
    children:[
      {path: '/a',name: 'A页面',component: APage},
      {path: '/b/:id',name: 'B页面',component: BPage},
      {path: '/bdetail/:id',name: 'C页面',component: CPage},
    ]
}    

建议后修改:(贴出代码帮我看看)
A列表页面跳转按钮:(breadNum记录面包屑层级)


B列表页面跳转按钮:

breadcrumb.vue页面:


状态管理store.js代码:

export default store = new Vuex.Store({
  state: {
    breadListState:[
      {name:'首页',path:'/'}
    ]
  },
  mutations: {
    breadListStateAdd(state,obj){
      state.breadListState.push(obj);
    },
    breadListStateRemove(state,num){
      state.breadListState.splice(num,state.breadListState.length-num);
    }
  }

})

路由route.js代码:

{
    path: '/',
    name: '首页',
    component: Main,
    redirect:'/home',
    children:[
      {path: '/a',name: 'A页面',component: APage},
      {path: '/b/:id',name: 'B页面',component: BPage,meta:{breadNumber:2}},
      {path: '/bdetail/:id',name: 'C页面',component: CPage,meta:{breadNumber:3}},
    ]
}    

===================================================贴下代码======================:
store.js

mutations: {
    breadListMutations(getters,list){
      getters.breadListState=list;
      sessionStorage.setItem('breadListStorage',list);
    }
},
  getters:{
    breadListState(){
      return JSON.parse(sessionStorage.getItem('breadListStorage')) || [];
    }
  }

breadcrumb.vue组件


页面中引用:


你可能感兴趣的:(vue面包屑(vue动态路由多级嵌套面包屑怎么弄))