How to check whether a subcomponent is mounted in Vue?
P粉401527045
P粉401527045 2023-12-26 19:49:44
0
1
395

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粉401527045
P粉401527045

reply all(1)
P粉826283529

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:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!