Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/async-component-vue-router.md
1# Do Not Use defineAsyncComponent with Vue Router23## Rule45Never use `defineAsyncComponent` when configuring Vue Router route components. Vue Router has its own lazy loading mechanism using dynamic imports directly.67## Why This Matters89Vue Router's lazy loading is specifically designed for route-level code splitting. Using `defineAsyncComponent` for routes adds unnecessary overhead and can cause unexpected behavior with navigation guards, loading states, and route transitions.1011## Bad Code1213```javascript14import { defineAsyncComponent } from 'vue'15import { createRouter, createWebHistory } from 'vue-router'1617const router = createRouter({18history: createWebHistory(),19routes: [20{21path: '/dashboard',22// WRONG: Don't use defineAsyncComponent here23component: defineAsyncComponent(() =>24import('./views/Dashboard.vue')25)26},27{28path: '/profile',29// WRONG: This also won't work as expected30component: defineAsyncComponent({31loader: () => import('./views/Profile.vue'),32loadingComponent: LoadingSpinner33})34}35]36})37```3839## Good Code4041```javascript42import { createRouter, createWebHistory } from 'vue-router'4344const router = createRouter({45history: createWebHistory(),46routes: [47{48path: '/dashboard',49// CORRECT: Use dynamic import directly50component: () => import('./views/Dashboard.vue')51},52{53path: '/profile',54// CORRECT: Simple arrow function with import55component: () => import('./views/Profile.vue')56}57]58})59```6061## Handling Loading States with Vue Router6263For route-level loading states, use Vue Router's navigation guards or a global loading indicator:6465```vue66<script setup>67import { ref } from 'vue'68import { useRouter } from 'vue-router'6970const router = useRouter()71const isLoading = ref(false)7273router.beforeEach(() => {74isLoading.value = true75})7677router.afterEach(() => {78isLoading.value = false79})80</script>8182<template>83<LoadingBar v-if="isLoading" />84<RouterView />85</template>86```8788## When to Use defineAsyncComponent8990Use `defineAsyncComponent` for:91- Components loaded conditionally within a page92- Heavy components that aren't always needed93- Modal dialogs or panels that load on demand9495Use Vue Router's lazy loading for:96- Route-level components (views/pages)97- Any component configured in route definitions9899## Key Points1001011. Vue Router and `defineAsyncComponent` are separate lazy loading mechanisms1022. Route components should use direct dynamic imports: `() => import('./View.vue')`1033. Use navigation guards for route-level loading indicators1044. `defineAsyncComponent` is for component-level lazy loading within pages105106## References107108- [Vue Router Lazy Loading Routes](https://router.vuejs.org/guide/advanced/lazy-loading.html)109- [Vue.js Async Components Documentation](https://vuejs.org/guide/components/async)110