vue2.0-处理边界情况

一,访问根实例 (new Vue)

this.$root.dataVal

二,访问父组件。维护扩展性低

this.$parent || this.$parent.$parent

三,访问子组件

this.$ref.refAttribute

四,依赖注入(处理组件层层嵌套的关系)

//父组件:提供给后代组件的数据或方法

provide: function () {
  return {
    getMap: this.getMap
  }
}

// 在后代组件,使用inject 属性来接收

inject: ['getMap']

//实际上,你可以把依赖注入看作一部分“大范围有效的 prop”,除了:

//祖先组件不需要知道哪些后代组件使用它提供的 property
//后代组件不需要知道被注入的 property 来自哪里

 

posted @ 2020-09-03 15:24  毛栗的demo  阅读(70)  评论(0)    收藏  举报