简体   繁体   English

Vue同级组件挂钩生命周期关系

[英]Vue sibling components hooks lifecycle relationships

Never used Vue.js before. 以前从未使用过Vue.js。 I have one parent component and 2 child components. 我有一个父组件和两个子组件。 These 2 child components should interact with asynchronous actions using the native Vue event bus system (with a dummy Vue object used as a shared container for the event bus). 这两个子组件应使用本机Vue事件总线系统(将虚拟Vue对象用作事件总线的共享容器)与异步操作进行交互。

Having something like the following: 具有以下内容:

EventBus.js EventBus.js

import Vue from "vue"
export default new Vue()

Parent.vue Parent.vue

import Child1 from "./Child1.vue"
import Child2 from "./Child2.vue"

export default {

    name: "Parent",

    components: {
        child1: Child1,
        child2: Child2,
    }

}

Child1.vue Child1.vue

import EventBus from "./EventBus"

export default {

    name: "Child1",

    beforeCreate () {

        EventBus.$once("MY_EVENT_X", async () => {
            EventBus.$emit("MY_EVENT_Y")
        })

    },

    mounted () {
        // something
    }

}

Child2.vue Child2.vue

import EventBus from "./EventBus"

export default {

    name: "Child2",

    beforeCreate () {

        EventBus.$once("MY_EVENT_Y", async () => {
            // do something 
        })

    },

    mounted () {
        EventBus.$emit("MY_EVENT_X")
    }

}

My question: having the events handlers defined in the "beforeCreate" hook, can I be sure that the "beforeCreate" hooks of both Child1 and Child2 components will be initiliazed before any of the "mounted" hooks of Child1 or Child2 will be called by Vue? 我的问题是:在“ beforeCreate”钩子中定义了事件处理程序,我可以确定在调用Child1或Child2的任何“已安装”钩子之前,将对Child1和Child2组件的“ beforeCreate”钩子进行初始化。 Vue的?

You can leverage component hook order between parent and children. 您可以利用父级和子级之间的组件挂钩顺序。 When parent mounted is called, we will be sure all child components is created and mounted. 调用父mounted ,我们将确保所有子组件均已创建并挂载。

在此处输入图片说明

Image source from here 图片来源从这里

To do it, you can define a boolean flag in parent, change this flag to true in mounted hook: 为此,您可以在父级中定义一个布尔标志,在挂接的钩子中将此标志更改为true:

import Child1 from "./Child1.vue"
import Child2 from "./Child2.vue"

export default {

    name: "Parent",

    components: {
        child1: Child1,
        child2: Child2,
    },
    data: () => ({
      isChildMounted: false
    }),
    mounted() {
      this.isChildMounted = true
    }
}

Make sure to pass this flag to children component: 确保将此标志传递给子组件:

<child-2 :isReady="isChildMounted" />

Finally, in child component, watching for props change. 最后,在子组件中,观察道具的变化。 When the flag is changed to true, we know all child components is ready. 当标志更改为true时,我们知道所有子组件均已准备就绪。 It's time to emit event. 现在该发出事件了。

import EventBus from "./EventBus"

export default {
    name: "Child2",
    props: ['isReady'],
    beforeCreate () {
      EventBus.$once("MY_EVENT_Y", async () => {
          // do something 
      })
    },
    watch: {
      isReady: function (newVal) {
        if (newVal) {
          // all child component is ready
          EventBus.$emit("MY_EVENT_X")
        }
      }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM