vue動態(tài)路由實現(xiàn)多級嵌套面包屑的思路與方法
前言
最近在工作中遇到了一個問題,是關(guān)于vue動態(tài)路由多級嵌套面包屑怎么弄(不是動態(tài)路由嵌套可以嘗試用 this.$route.matched方法獲取到path和name集合,動態(tài)的嵌套獲取不到全部具體的id)
功能比如:A列表頁面路由如/a,點擊任意一列進入任意一個A的詳情頁面名字為B,/b/03(這個是動態(tài)路由弄是吧,03就是id嘛),點擊B頁面任意一列,再進入B的詳情頁名字為C,路由如/bdetail/01;現(xiàn)在弄面包屑要獲取到的路由是剛剛打開的,如(/a;/b/03;/bdetail/01)
思路:獲取所有進入的層級的路由和名稱如breadlist=[{path:'/a',name:'一級'},{path:'/b/03',name:'二級'},{path:'/bdetail/01',name:'三級'}] ,然后遍歷出來如: <span v-for="(item in breadlist)"><router-link :to="item.path">{{item.name}}</router-link></span>
做法
下面貼出相關(guān)代碼:
A列表頁面跳轉(zhuǎn)按鈕:(breadNum記錄面包屑層級)
<router-link :to="{path:'/b/'+id,query:{breadNum:2}}"></router-link>
B列表頁面跳轉(zhuǎn)按鈕:
<router-link :to="{path:'/bbdetail/'+id,query:{breadNum:3}}"></router-link>
breadcrumb.vue頁面:
<template>
<div class="breadbox">
<span v-for="(item,index) in breadlist" >
<router-link :to="item.path">{{item.name}}</router-link>
</span>
</div>
</template>
<script>
export default{
created() {
this.getBreadcrumb();
},
data() {
return {
breadlist: '' // 路由集合
}
},
methods: {
getBreadcrumb() {
var breadNumber= this.$route.query.breadNum || 1;//url變量breadNum記錄層級,默認為1,如果大于1,要添加上變量;
var breadLength=this.$store.state.breadListState.length;//目前breadlist集合數(shù)組個數(shù)
var curName=this.$route.name;
var curPath=this.$route.fullPath;
var newBread={name:curName,path:curPath};
var ishome=curName=='首頁';
console.log(ishome);
if(breadNumber===1){//點擊一級菜單
this.$store.commit('breadListStateRemove',1);//初始化,只有首頁面包屑按鈕
if(!ishome)//如果不是首頁
this.$store.commit('breadListStateAdd',newBread);//當(dāng)前頁面添加到breadlist集合
}
else if(breadLength<=breadNumber){//如果不是一級導(dǎo)航,并且breadlist集合個數(shù)等于或者小于目前層級
this.$store.commit('breadListStateAdd',newBread);//要把當(dāng)前路由添加到breadlist集合
}else{
this.$store.commit('breadListStateRemove',parseInt(breadNumber)+1);//如果往回點面包屑導(dǎo)航,截取;
}
this.breadlist=this.$store.state.breadListState;
console.log(this.breadlist);
}
},
watch: {
$route () {
this.getBreadcrumb();
}
},
}
</script>
狀態(tài)管理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},
]
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
在vue3中使用el-tree-select實現(xiàn)樹形下拉選擇器效果
el-tree-select是一個含有下拉菜單的樹形選擇器,結(jié)合了?el-tree?和?el-select?兩個組件的功能,這篇文章主要介紹了在vue3中使用el-tree-select做一個樹形下拉選擇器,需要的朋友可以參考下2024-03-03
electron中使用本地數(shù)據(jù)庫的方法詳解
眾所周知,electron是可以開發(fā)桌面端的框架,那我們有一些數(shù)據(jù)不想讓別人看到,只能在自己的電腦上展示時怎么辦呢,這個時候就可以用到本地數(shù)據(jù)庫,本文將以sqlite3為例介紹一下electron如何使用本地數(shù)據(jù)庫2023-10-10
Vue基于iview table展示圖片實現(xiàn)點擊放大
這篇文章主要介紹了Vue基于iview table展示圖片實現(xiàn)點擊放大,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
vue axios post發(fā)送復(fù)雜對象問題
現(xiàn)在vue項目中,一般使用axios發(fā)送請求去后臺拉取數(shù)據(jù)。這篇文章主要介紹了vue axios post發(fā)送復(fù)雜對象的一點思考,需要的朋友可以參考下2019-06-06

