Home  >  Q&A  >  body text

How to check whether a subcomponent is mounted in Vue?

I want to check if the child component is installed and I want to move that information to the parent component. For this I'm using launch. So the example here is my parent component:

<child @is-child-mounted="childMounted" />

export default {
  data() {
    return {
      childMounted: false,
    };
  },

  mounted() {
    if (this.childMounted) {
      //do something
    }
  },
}

In the child component I changed "is-child-mounted" to true:

mounted() {
    this.$emit('isChildMounted', true);
  },

But if (this.childMounted) is still false. So how to check the parent component if the child component is mounted?

P粉401527045P粉401527045297 days ago400

reply all(1)I'll reply

  • P粉826283529

    P粉8262835292023-12-27 12:33:19

    You can add listeners on child components of a parent component. It looks like this:

    Vue3
          <Component
            @vnodeMounted="handleMounted"
          />
    
    Vue2
          <Component
             @hook:mounted="handleMounted"
          />
    

    You can replace the hook name with the lifecycle name you want to listen to! I imagine it should be rarely used, since it doesn't appear in the documentation and is therefore an internal API, destined not to be used directly.

    source:

    reply
    0
  • Cancelreply