Javascript is required
ยท
0 min read

Vue Tip: Speed Up Initial Load Using Async Components

Vue Tip: Speed Up Initial Load Using Async Components Image

The traditional, synchronous way of loading components is:

import MyComponent from './MyComponent.vue'

In large applications, it makes sense to split the code into smaller chunks and only load it from the server when needed.

Vue 3 therefore provides the defineAsyncComponentfunction:

1import { defineAsyncComponent } from 'vue'
2
3const AsyncComp = defineAsyncComponent(() => {
4  return new Promise((resolve, reject) => {
5    // ...load component from server
6    resolve(/* loaded component */)
7  })
8})

Now we can use AsyncComp as a standard component in Vue.

Here are the docs for Async Components in Vue 3.

The syntax for Vue 2 is not that different:

const AsyncComponent = () => import('./components/LazyLoadingFTW.vue')

If you liked this Vue tip, follow me on X to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter:

I will never share any of your personal data. You can unsubscribe at any time.