Vue: Action event on loaded component Async

Created on 4 Feb 2019  路  5Comments  路  Source: vuejs/vue

What problem does this feature solve?

Reference: https://vuejs.org/v2/guide/components-dynamic-async.html#Handling-Loading-State

Current Issue: We do not know when the async component was loaded, with this, the 'Ref' is also not recognized in the 'mounted()' father

What does the proposed API look like?

Feature Request: Prop from 'created' in asynchronous component to go event

const AsyncComponent = () => ({
  // The component to load (should be a Promise)
  component: import('./MyComponent.vue'),
  // A component to use while the async component is loading
  loading: LoadingComponent,
  // A component to use if the load fails
  error: ErrorComponent,
  // Delay before showing the loading component. Default: 200ms.
  delay: 200,
  // The error component will be displayed if a timeout is
  // provided and exceeded. Default: Infinity.
  timeout: 3000,
  created: () => something()
})

Most helpful comment

You can add a listener on the child component:

<ChildAsync @ready="doSomething"/>

Then in the child's mounted hook do this.$emit('ready')

All 5 comments

If something needs to happen after the component is loaded, it should be placed inside that component's own lifecycle hook. If that doesn't suit your use case you need to provide more context instead of a single sentence that I can barely understand.

Sorry about my English.
I meant that it has no way of knowing when an asynchronous component was loaded.
The parent component does not recognize the role or property of the child component in the mounted method because the child component is asynchronous.

Example:

<template>
  <div>
    <ChildAsync ref="child"/>
  </div>
</template>

<script>
export default {
  name: "Example",
  components: {
    ChildAsync: () => import("./childExample")
  },
  mounted() {
    const { test } = this.$route.query;
    if (test === 'ok') {
      this.$refs.child.someFunction(); // *ERROR* -> undefined
    }
  }
};
</script>

You can add a listener on the child component:

<ChildAsync @ready="doSomething"/>

Then in the child's mounted hook do this.$emit('ready')

All right, thank you!

I found a cleaner way to do this (without modify child component):

<ChildAsync @hook:mounted="doSomething"/>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

julianxhokaxhiu picture julianxhokaxhiu  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments

franciscolourenco picture franciscolourenco  路  3Comments

paceband picture paceband  路  3Comments

gkiely picture gkiely  路  3Comments