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.方法
青春承载希望,奋斗成就未来
原文地址:https://www.cnblogs.com/ckmouse/p/11467048.html