[关闭]
@Bios 2018-12-10T08:38:44.000000Z 字数 2966 阅读 877

Vue 与 React 父子组件之间的家长里短

Vue react


Vue

  1. // father.js
  2. <template>
  3. <div id="father">
  4. 这是父组件:
  5. <p>父组件</p>
  6. <Child ref="child" :msg="msg" @click="faClick"></Child>
  7. </div>
  8. </template>
  9. <script>
  10. import Child from './child';
  11. export default {
  12. data() {
  13. return {
  14. msg: '父组件传给子组件' // 传递给子组件的值
  15. };
  16. },
  17. components: {
  18. Child
  19. },
  20. methods: {
  21. faClick(msg) { // msg 子组件传递给父组件的值
  22. alert(msg);
  23. },
  24. childSayHello() { // 父组件调用子组件的方法
  25. this.$refs,child.sayHello();
  26. }
  27. }
  28. }
  29. </script>
  1. // child.js
  2. <template>
  3. <div id="child">
  4. 这是子组件:<p>父组件传递的值:{{msg}}</p>
  5. <button @click="click">接收父组件方法</button>
  6. </div>
  7. </template>
  8. <script>
  9. export default {
  10. props: ['msg'],
  11. data() {
  12. return {
  13. childMsg : '哈哈哈'
  14. };
  15. },
  16. methods: {
  17. click() {
  18. this.$emit('click',this.childMsg); // 第一个参数为派发的事件名, 第二个参数为传递的值
  19. },
  20. sayHello() {
  21. alert('I am child!');
  22. }
  23. }
  24. }
  25. </script>

父组件向子组件传值:

  1. 在父组件中引入并注册子组件
  2. 在子组件中定义 props:['msg'] (不能省略引号)
  3. 通过 :msg="msg" 的方法传递变量,也可以通过 msg="msg" 传递字符串

父组件调用子组件的方法:

  1. 在父组件中给子组件绑定一个 ref="xxx" 属性
  2. 通过 this.$refs.xxx.方法 调用

子组件向父组件传值:

  1. 在子组件中定义一个方法
  2. 通过 this.$emit('事件名','参数') 派发一个事件,并传递参数
  3. 父组件中通过 @事件名 的方式监听事件
  4. 父组件中定一个一个方法,该方法的参数对应子组件传递过来的参数

子组件调用父组件的方法:

子组件可以通过this.$parent.xxx 直接调用父组件的方法。

通过子组件派发的事件,不仅可以向父组件传递参数,父组件也可以通过传递的参数,改变向子组件传递的值,从而改变子组件。

props 还可以进行一系列的格式校验,更多内容查看官网

  1. props: {
  2. // 基础的类型检查 (`null` 匹配任何类型)
  3. propA: Number,
  4. // 多个可能的类型
  5. propB: [String, Number],
  6. // 必填的字符串
  7. propC: {
  8. type: String,
  9. required: true
  10. },
  11. // 带有默认值的数字
  12. propD: {
  13. type: Number,
  14. default: 100
  15. },
  16. // 带有默认值的对象
  17. propE: {
  18. type: Object,
  19. // 对象或数组且一定会从一个工厂函数返回默认值
  20. default: function () {
  21. return { message: 'hello' }
  22. }
  23. },
  24. // 自定义验证函数
  25. propF: {
  26. validator: function (value) {
  27. // 这个值必须匹配下列字符串中的一个
  28. return ['success', 'warning', 'danger'].indexOf(value) !== -1
  29. }
  30. }
  31. }

React

  1. // father.js
  2. import React, { Component } from 'react'
  3. import Child from './child.js';
  4. class Father extends Component {
  5. constructor(props) {
  6. super(props);
  7. this.state = {
  8. con: '父组件给子组件'
  9. }
  10. }
  11. // 传递给子组件的方法,并接收子组件实例,绑定在this.child上
  12. onRef = (ref) => {
  13. this.child = ref
  14. }
  15. // 通过this.child 就可以直接调用子组件的内部方法
  16. click = () => {
  17. this.child.sayHello();
  18. }
  19. // 传递个子组件的方法
  20. faClick = (msg) => {
  21. alert(msg);
  22. }
  23. render() {
  24. return (
  25. <div>
  26. <p>这是父组件</p>
  27. <button onClick={this.click}>调用子组件方法</button>
  28. <div>
  29. 这是子组件
  30. <Child onRef={this.onRef} connect={this.state.con} click={(msg) => this.faClick(msg)}/>
  31. </div>
  32. </div>
  33. )
  34. }
  35. }
  36. export default Father;
  1. // child.js
  2. import React, { Component } from 'react'
  3. class Child extends Component {
  4. constructor(props) {
  5. super(props);
  6. }
  7. // 调用父组件传递的方法,并传递子组件实例
  8. componentDidMount(){
  9. this.props.onRef(this);
  10. }
  11. // 调用父组件传递的方法
  12. click= ()=> {
  13. this.props.click('哈啊哈');
  14. }
  15. // 子组件内部方法
  16. sayHello = () => {
  17. alert('I am child');
  18. }
  19. render() {
  20. return (
  21. <div>
  22. <p>{this.props.connect}</p>
  23. <button onClick={this.click}>接收父组件的方法</button>
  24. </div>
  25. )
  26. }
  27. }
  28. export default Child;

父组件向子组件传值:

  1. 在父组件中引入子组件
  2. 通过 connect={this.state.con} 方式可以传递值
  3. 子组件通过 this.props.connect 接收

父组件调用子组件的方法:

  1. 给子组件传递一个方法 onRef={this.onRef}
  2. 子组件在 componentDidMount 生命周期里 调用这个方法,并回传自身实例
  3. 父组在该方法中接收子组件实例,并挂载在父组件实例属性上,例:this.child = ref
  4. 最后就可以通过 this.child.xxx 直接调用子组件方法

子组件向父组件传参:

  1. 在父组件中给子组件传递一个方法,click={(msg) => this.faClick(msg)}
  2. 在子组件中通过一个事件接收这个方法,onClick={this.click}
  3. 通过click= ()=> {this.props.click('哈啊哈');} 从而传递参数

子组件调用父组件方法

  1. 父组件可以直接传递一个方法给子组件
  2. 子组件可以通过 this.props.xxx 调用

不能直接通过 <button onClick={this.props.click('哈啊哈')}>接收父组件的方法</button> 进行传参,这样在组件初始化时,事件就执行了。

Vue 与 React 的不同:

  1. React 的子组件中不用定义父组件传值对应的变量
  2. React 的子组件不用派发事件,父组件可以直接传递方法
  3. 子组件通过this.props.click 可以调用父组件传递的方法,并传参
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注