ts ref的使用

ref的作用在于父组件取到子组件的所有state内容。

在父组件中 编写如下:

1.  public child: any; 类中定义child,用于存放子组件的作用域

2. 绑定子组件作用域

public onRef(ref: any) {
this.child = ref
}
3. 子组件中绑定ref   <Childpage  
onRef={this.onRef}  /
>
4. onRef  绑定this      this.onRef = this.onRef.bind(this)
 
在子组件中 编写如下:
  在constructor 中  onRef 绑定this 
this.props.onRef(this)
 之后在 父组件中可以随便调用子组件的state中的值 , 调用方法 
  this.child.state.......
posted @ 2019-05-24 10:40  Denvue  阅读(5202)  评论(0编辑  收藏  举报