美文网首页
04【手写vue-router】注入全局属性

04【手写vue-router】注入全局属性

作者: 章音十 | 来源:发表于2020-04-27 17:45 被阅读0次

给组件注入this.$routerrhis.$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;
        }
      }
  });
}

相关文章

网友评论

      本文标题:04【手写vue-router】注入全局属性

      本文链接:https://www.haomeiwen.com/subject/exruwhtx.html