美文网首页
vue 父子组件相互通信

vue 父子组件相互通信

作者: 付蔷蔷 | 来源:发表于2020-08-18 16:54 被阅读0次

父子组件通信包括:

  1. 父组件调用子组件的方法、子组件调用父组件的方法、
  2. 父组件改变子组件的属性值、子组件改变父组件的属性值。

细分起来这是四种情况,其实可以总结为两种:
1. 父组件通过调用子组件方法改变子组件的属性值、
2.子组件通过调用父组件方法改变子组件值

换言之,明白怎么父调子方法,子调父方法,就知道怎么改变属性值了。

父调子:

1. 通过给子组件设置ref

// 父组件 father.vue:
<template>
  <div class="father">
    I'm {{name}}, and {{sonName}}'s father
    <child :father="name" ref="child" />
    <button @click="changeName('大头')"></button>
   </div>
</template>
<script>
import child from "./child"
export default {
  name: "father",
  data () {
    return {
      name: "小头爸爸",
      sonName: "",
      changeName: null
      }
   },
   mounted () {
      this.sonName = this.$refs.child.name;
      this.changeName = this.$refs.child.changeName;
   },
  methods: {
      showFatherName () {
         alert(`I'm a father, call me ${this.name}`)
      }
   },
    components: {
        child
      }
    }
</script>  
    // 子组件 child.vue
    <template>
        <div>
            I'm {{name}}
        </div>
    </template>
    <script>
        export default {
            name: 'child',
            data () {
                return {
                    name: "大头儿子"
                }
            },
            methods: {
                changeName (name) {
                    this.name = name
                }
            }
        }
    </script>

这样通过vm.$refs.child直接就能调用子组件的方法改变子组件属性,或者使用子组件的某些变量。 以上点击按钮,就可以通过调用子组件的changeName改变子组件的name了,效果如下图


父调子.gif

子调父:

1. 通过 vm.$parent 直接调用

    // 父组件
    <template>
        <div class="father">
            I'm {{name}}, and {{sonName}}'s father
            <child :fatherName="name" />
            <button @click="changeName('大头')"></button>
        </div>
    </template>
    <script>
        import child from "./child"
        export default {
            name: "father",
            data () {
                return {
                    name: "小头爸爸",
                    sonName: "",
                    changeName: null
                }
            },
            methods: {
                showName () {
                    alert(`I'm a father, call me ${this.name}`)
                }
            },
            components: {
                child
            }
        }
    </script> 
    // 子组件 child.vue
    <template>
        <div>
            I'm {{name}},and {{fatherName}}'s son.
            <button @click="changeName">点击</button>
        </div>
    </template>
    <script>
        export default {
            name: 'child',
            data () {
                return {
                    name: "大头儿子"
                }
            },
            methods: {
                changeName (name) {
                    return this.$parent.showName()
                }
            }
        }
    </script>

2. 通过 props 引用父组件的方法

    // 父组件 father.vue:
    <template>
        <div class="father">
            I'm {{name}}, and {{sonName}}'s father
            <child :fatherName="name" changeName="showName" />
        </div>
    </template>
    <script>
        import child from "./child"
        export default {
            name: "father",
            data () {
                return {
                    name: "小头爸爸",
                    sonName: "",
                    changeName: null
                }
            },
            methods: {
                showName () {
                    alert(`I'm a father, call me ${this.name}`)
                }
            },
            components: {
                child
            }
        }
    </script>  
    // 子组件 child.vue
    <template>
        <div>
            I'm {{name}}, and {{fatherName}}'s son.
            <button @click="changeName">点击</button>
        </div>
    </template>
    <script>
        export default {
            name: 'child',
            data () {
                return {
                    name: "大头儿子"
                }
            },
            props: {
                fatherName: {
                    type: String
                },
                changeName: {
                    type: String
                }
            }
        }
    </script>

通过 $emit 调用父组件的方法

     // 父组件 father.vue:
    <template>
        <div class="father">
            I'm {{name}}, and {{sonName}}'s father
            <child :fatherName="name" @showName="showName" />
        </div>
    </template>
    <script>
        import child from "./child"
        export default {
            name: "father",
            data () {
                return {
                    name: "小头爸爸",
                    sonName: ""
                }
            },
            methods: {
                showName (name) {
                    alert(`I'm a father, call me ${name}`)
                }
            },
            components: {
                child
            }
        }
    </script>  
    // 子组件 child.vue
    <template>
        <div>
            I'm {{name}}, and {{fatherName}}'s son.
            <button @click="changeName">点击</button>
        </div>
    </template>
    <script>
        export default {
            name: 'child',
            data () {
                return {
                    name: "大头儿子"
                }
            },
            props: {
                fatherName: {
                    type: String
                }
            },
            methods: {
                changeName: {
                    return this.$emit("showName", "小头爸爸")
                }
            }
        }
    </script>

三种方式实现的效果一样,图如下


子调父.gif

以上是父子组件相互间调用方法的例子,如果想子改变父变量或者父改变子的变量,直接在调用的方法里修改就行。

相关文章

  • Vue相关知识点

    1、vue父子组件之间的通信 在vue组件通信中其中最常见通信方式就是父子组件之中的通性,而父子组件的设定方式在不...

  • Vue如何实现组件通信?

    Vue组件通信的三种情况: 父子通信 爷孙通信 兄弟通信 父子通信:父组件使用Prop向子组件传递数据,子组件通过...

  • vue组件间通信的一些实用方法(VUE2.x)

    vue组件间通信的一些实用方法(VUE2.x) 一、父子组件间通信 常用的父子组件通信方法,一般涉及props和$...

  • vue2中eventbus遇到的坑

    前言 vue组件非常常见的有父子组件通信,兄弟组件通信。而父子组件通信就很简单,父组件会通过 props 向下传数...

  • Vue事件总线(EventBus)使用详细介绍

    前言 vue组件非常常见的有父子组件通信,兄弟组件通信。而父子组件通信就很简单,父组件会通过 props 向下传数...

  • Vue事件总线(EventBus)

    vue组件非常常见的有父子组件通信,兄弟组件通信。而父子组件通信就很简单,父组件会通过props向下传数据给子组件...

  • VUE - EventBus

    vue组件非常常见的有父子组件通信,兄弟组件通信。而父子组件通信就很简单,父组件会通过 props 向下传数据给子...

  • VUE组件(传值,生命周期)

    VUE生命周期 VUE子传父组件通信 VUE非父子组件传值

  • 09-生命周期及非父子组件间的通信

    一. Vue生命周期 二、生命周期代码 三、非父子组件通信 vue中非父子组件通信需要借助一个空的vue实例,案...

  • Vue组件通信

    Vue 组件之间的通信,通常我们遇到的都是父子组件之间的通信 一、父子组件传参 子组件定义 Props 属性; 父...

网友评论

      本文标题:vue 父子组件相互通信

      本文链接:https://www.haomeiwen.com/subject/xunbdktx.html