Parent组件

import React from "react";
import Child from "./component/Child"; class Parent extends React.Component {
render() {
return (
<div>
我是父组件
<Child childEvevnt={this.childEvevnt} />
<button onClick={this.triggerEvevt}>触发子</button>
</div>
);
}
// 此事件接收子对象
childEvevnt = childDate => {
this.$child = childDate;
};
// 父组件触发子组件的事件
triggerEvevt = () => {
this.$child.alertEvevnt();
};
} export default Parent;

Child组件

import React from "react";
class Child extends React.Component {
render() {
return <div>我是子组件</div>;
}
componentDidMount() {
this.props.childEvevnt(this);
}
// 父组件要触发的事件
alertEvevnt = () => {
alert("父呼唤我呢!!");
};
}
export default Child;

注意点:

1.使用箭头函数,小心this指向有差错

  ()=>  {  }

2.父组件通过props传参把子组件对象接收过来

   <Child childEvevnt={this.childEvevnt} />

3.子组件内部进行传递

  componentDidMount() {
    this.props.childEvevnt(this);
  }
      4.父组件把接收过来的子对象绑定到父自定义事件上
  childEvevnt = childDate => {
    this.$child = childDate;
  };
      5.父组件内部触发子组件的事件
        this.$child 上有了子组件的事件
      6.触发
  triggerEvevt = () => {
    this.$child.alertEvevnt();
  };
     

React 父组件触发子组件事件的更多相关文章

  1. vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式

    vue-property-decorator和typescript结合构建的class类组件,父组件触发子组件方法的方式 class类组件示例 Father类组件 <template> & ...

  2. [转] vue父组件触发子组件事件

    1. 父组件中获取子组件方法 $children 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <template>     < ...

  3. vue.js中父组件触发子组件中的方法

    知识点:vue.js中,父组件的method中,触发子组件中的方法,获得子组件中的定义的属性 (1)子组件 : child_crud.js var html_child_crud= "< ...

  4. 基于vue,通过父组件触发子组件的请求,等请求完毕以后,显示子组件,同时隐藏父组件

    正常情况下,子组件应该尽量减少业务逻辑,而应该将业务逻辑放到父组件里面,从而减少耦合,但是当 我们不得不用到这种情况时,可以采用下面的思路 解决方案 尽量将请求单独作为一个函数(不要将请求放到show ...

  5. vue父组件触发子组件方法

    比如应用场景是弹窗中的组件,想要点弹窗时更新该组件展示对应记录的的值 methods: { edit (record) { this.mdl = Object.assign({}, record) t ...

  6. Vee-validate 父组件获取子组件表单校验结果

    vee-validate 是为 Vue.js 量身打造的表单校验框架,允许您校验输入的内容并显示对应的错误提示信息.它内置了很多常见的校验规则,可以组合使用多种校验规则,大部分场景只需要配置就能实现开 ...

  7. vue 父组件与子组件的三生三世

    父组件和子组件相互传值:https://www.cnblogs.com/cxscode/p/11187989.html vue父组件触发子组件方法:https://www.cnblogs.com/cx ...

  8. 042——VUE中组件之子组件使用$on与$emit事件触发父组件实现购物车功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. vue 关于父组件无法触发子组件的事件的解决方法

    一般情况导致无法触发子组件的方法  基本都是由于子组件未渲染完成 就进行了调用,解决方法如下: 1.加定时器  setTimeout(() => { //加定时器原因是,子组件页面未渲染处理就做 ...

随机推荐

  1. Ssh safe

    新建用户,设置密码 useradd eason passwd eason 不允许root直接登陆 修改配置文件 vi /etc/ssh/sshd_config 禁止root登录 查找“#PermitR ...

  2. js中国各大城市快速选择代码

    js中国各大城市快速选择插件 在线演示本地下载

  3. WordPress的wordfence插件的设置方法

  4. Spring学习笔记之基础、IOC、DI(1)

    0.0 Spring基本特性 Spring是一个开源框架:是基于Core来架构多层JavaEE系统 1.0 IOC 控制反转:把对象的创建过程交给spring容器来做. 1.1 application ...

  5. boost的单例模式

    template <typename T> struct singleton_default {   private:     struct object_creator     {    ...

  6. UBuntu安裝使用PIP

    Windows下安裝python包還是比較方便的,直接在FLD網站下載對應的EXE文件就可以安裝,在linux系統下,使用pip,easy egg 管理工具可以減輕安裝負擔. 原文鏈接:http:// ...

  7. 统计之都 http://cos.name/

    http://cos.name/ IMS:一个洲际人际交流网络(为学生免费提供会员资格) 原文链接:http://cos.name/2014/07/ims-a-cross-continent-huma ...

  8. Resolving Strong Reference Cycles for Closures

    You resolve a strong reference cycle between a closure and a class instance by defining a capture li ...

  9. JS 封装一个判断闰年平年的方法 aa(nian)

    nn(2017) function nn (nian){ if(nian%4 == 0 && nian%100 !== 0 || nian%400 ==0 ) { alert(&quo ...

  10. 何为JQuery对象?