Lazy Loading Components
Learn about lazy loading components and how to manage these in our application using the defineAsyncComponent function.
We'll cover the following...
Not every component is equal. Some are more important and should be displayed immediately, while content further down the page won’t be visible right away anyway, so it can be loaded a bit later. Similarly, content that’s shown in modals and popups, especially if there’s a lot of it, doesn’t need to be loaded upfront.
Async components
There’s a slight difference between how async components are defined in Vue 2 and Vue 3. In Vue 2, async components can be used by providing a function that returns an object like this:
The defineAsyncComponent function
However, in Vue 3, we need to use the defineAsyncComponent function. Besides changing the names of some properties, a few other properties were added, namely suspensible and onError.
Let’s look at how we can implement a lazy-loaded component. We need the PanelModal, PanelModalContent, LoadingComponent, ErrorComponent, and PanelModalExample components for this example.
Let’s start with the PanelModal component:
The PanelModal component is a div that slides from the right side of the screen when the ...