vue.js 父组件主动获取子组件的数据和方法、子组件主动获取父组件的数据和方法

父组件主动获取子组件的数据和方法
1.调用子组件的时候 定义一个ref

  <headerchild ref="headerChild"></headerchild>

  在父组件里面通过

  this.$refs.headerChild.属性
  this.$refs.headerChild.方法

2.子组件主动获取父组件的数据和方法   element-ui中 不行
  在子组件里面通过

  this.$parent.属性
  this.$parent.方法

3. 子组件获取父组件(无层级限制)

父组件:

provide() {
    return {
      template: this
    }
  }
 
子组件
inject: ['template']
 
子组件通过this.template.属性    this.template.方法
posted @ 2019-09-05 16:41  青春无敌小宇宙  阅读(1268)  评论(0编辑  收藏  举报