给组件注入this.$router
和rhis.$route
// ... 省略一些代码
myRouter.install = (Vue, options)=>{
Vue.mixin({
beforeCreate(){
// 判断是否为根组件,如果是根组件就直接从options里取router
if(this.$options && this.$options.router){
this.$router = this.$options.router;
this.$route = this.$router.routeInfo;
}else{
// 如果是子组件,就把父组件的router给它
this.$router = this.$parent.$router;
this.$route = this.$router.routeInfo;
}
}
});
}
网友评论