4

I am wondering how to check when an async component has loaded.

Normally I would load my component like so:

Vue.component(
     'booking-overlay', 
     () => import('@/components/BookingOverlayAsync.vue')
);

Using something like this does not work:

Vue.component(
   'booking-overlay',
    () => import('@/components/BookingOverlayAsync.vue').then(() => {
        console.log('component has loaded');
    })
);

It leads to the following error:

Uncaught (in promise) TypeError: Cannot read property '__esModule' of undefined at ensureCtor..

The component no longer loads but the promise does resolve though. By adding the then it no longer resolves the promise itself it seems.

How can I globally register this async component and also check when this components chunk of JavaScript has been loaded?

Obviously I could simply do this together with setting the global component:

import('@/components/BookingOverlayAsync.vue').then(() => {
    console.log('Chunk loaded');
});

This just seems like a very ugly solution though.

1
  • Can you show the code of targeted vue component you are going to load asynchronously Commented Oct 19, 2018 at 8:31

1 Answer 1

5

Because your second promise object returned by .then function doesn't continue to hand down the async loaded component to vue lib itself.

the Vue.component function expects that the second parameter should be a function that returns a promise where the resolved data must be the async-loaded component itself.

please try following:

Vue.component(
        'booking-overlay',
        () => import('@/components/BookingOverlayAsync.vue').then(component => {
            console.log('component has loaded');
            return component;
        })
    );

Sign up to request clarification or add additional context in comments.

1 Comment

That was a lot more obvious than I thought it would be. It works now. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.