vue2.0 elementUI制作面包屑導航欄
更新時間:2018年02月22日 09:49:13 投稿:laozhang
本篇文章主要給大家詳細代碼講解了vue2.0 elementUI制作面包屑導航欄的過程,對此有興趣的朋友可以學習下。
Main.js
var routeList = [];
router.beforeEach((to, from, next) => {
var index = -1;
for(var i = 0; i < routeList.length; i++) {
if(routeList[i].name == to.name) {
index = i;
break;
}
}
if (index !== -1) {
//如果存在路由列表,則把之后的路由都刪掉
routeList.splice(index + 1, routeList.length - index - 1);
} else if(to.name != '登錄'){
routeList.push({"name":to.name,"path":to.fullPath});
}
to.meta.routeList = routeList;
next()
});
2、在要使用的組件中
<template>
<div class="level-bread">
<el-breadcrumb separator="/">
<el-breadcrumb-item v-for="item in realList" :to="item.path">{{item.name}}</el-breadcrumb-item>
</el-breadcrumb>
</div>
</template>
<script>
export default {
name: "lelve-bread",
created(){
this.getRoutePath();
},
data() {
return {
realList: []
}
},
methods:{
getRoutePath() {
this.realList = this.$route.meta.routeList;
}
},
beforeRouteEnter(to,from, next) {
next((vm) => {
vm.realList = to.meta.routeList;
});
},
// watch:{
// $route:function(newV,oldV) {
// this.realList =newV.meta.routeList;
// }
// }
}
</script>
用 watch 或者 beforeRouteEnter 均可。
需要注意的是,beforeRouteEnter 此時訪問不到this。
const Foo = {
template: `...`,
beforeRouteEnter (to, from, next) {
// 在渲染該組件的對應路由被 confirm 前調(diào)用
// 不!能!獲取組件實例 `this`
// 因為當守衛(wèi)執(zhí)行前,組件實例還沒被創(chuàng)建
},
beforeRouteUpdate (to, from, next) {
// 在當前路由改變,但是該組件被復用時調(diào)用
// 舉例來說,對于一個帶有動態(tài)參數(shù)的路徑 /foo/:id,在 /foo/1 和 /foo/2 之間跳轉(zhuǎn)的時候,
// 由于會渲染同樣的 Foo 組件,因此組件實例會被復用。而這個鉤子就會在這個情況下被調(diào)用。
// 可以訪問組件實例 `this`
},
beforeRouteLeave (to, from, next) {
// 導航離開該組件的對應路由時調(diào)用
// 可以訪問組件實例 `this`
}
}
以上就是本次我們整理的全部內(nèi)容,希望能夠幫助到大家,感謝大家對腳本之家的支持。
您可能感興趣的文章:
相關文章
vue3(ts)類型EventTarget上不存在屬性value的問題
這篇文章主要介紹了vue3(ts)類型EventTarget上不存在屬性value的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
vue3+ts重復參數(shù)提取成方法多處調(diào)用以及字段無值時不傳字段給后端問題
在進行API開發(fā)時,優(yōu)化參數(shù)傳遞是一個重要的考量,傳統(tǒng)方法中,即使參數(shù)值為空,也會被包含在請求中發(fā)送給后端,這可能會導致不必要的數(shù)據(jù)處理,而優(yōu)化后的方法則只會傳遞那些實際有值的字段,從而提高數(shù)據(jù)傳輸?shù)挠行院秃蠖颂幚淼男?/div> 2024-10-10最新評論

