Vue.js & Composition API (50 Qs)•Part 3: Advanced Internals, Scale & Production Architecture (Senior / Lead)
Dynamic and Async Components in Vue 3: `<component :is="...">` and `defineAsyncComponent`.
senior LevelCode ExampleComponentsAsync & PromisesSystem Design
In Simple Words:
- Dynamic Components (
<component :is="view">): Dynamically swaps active components based on reactive state or tabs. - Async Components (
defineAsyncComponent): Splits large components into separate lazy bundles loaded on demand, with built-in loading and error fallback states.
typescript
import { defineAsyncComponent, shallowRef } from 'vue';
const AsyncChart = defineAsyncComponent({
loader: () => import('./HeavyAnalyticsChart.vue'),
loadingComponent: LoadingSpinner,
errorComponent: ErrorFallback,
delay: 200, // Display loading component after 200ms delay
timeout: 10000, // Timeout error triggered after 10s
});
// Using shallowRef to store component definitions prevents unnecessary deep reactivity
const currentTab = shallowRef(AsyncChart);