这里的功能的是在浏览器的刷新时判断路由从而显示子导航栏。
可是bus.$on()
事件并没有监听到,我感觉是因为我放置的生命周期不正确,反正几个生命周期除了destroyed
我都试过了。。是不是我的思路有问题?
// router.js
// 页面刷新判断是否需要展示子导航
router.beforeEach((to, from, next) => {
const pathTo = to.path;
if (pathTo.match('/userManage')) {
bus.$emit('showChildNav', 'user');
} else if (pathTo.match('/contentManage')) {
bus.$emit('showChildNav', 'content');
}
next();
});
// header.vue
export default {
data () { ... },
method: {
showChild(type) {
// this.navChild.concat(this.userManage); 为什么不生效
if (type === 'user') {
this.$set(this, 'navChild', this.userManage);
} else if (type === 'content') {
this.$set(this, 'navChild', this.contentManage);
}
this.secondNav = true;
},
},
created() {
bus.$on('showChildNav', (type) => {
console.log('12213', type);
this.showChild(type);
});
},
}
如果要实现这个功能,应该怎么做?
試了下發現針對組件的鉤子才能這樣使用 (
beforeRouteEnter
)...看了下源碼,
beforeEach
會建立組件前先跑,所以沒辦法這樣搞,我這邊另外想個解決辦法是利用$route
的meta
,情況會比較簡單jsFiddle