loadRouteLocation() | Vue Router - Ensure Route Components Are Ready ๐ โ
Preload route components - Make sure your routes are fully loaded before rendering!
๐ฏ Function Overview โ
The loadRouteLocation() function is a powerful utility that ensures a route is completely loaded and ready to be rendered. This is particularly useful when you need to pass route components as props to <RouterView> or when working with advanced routing scenarios.
๐ Basic Usage โ
function loadRouteLocation(route: RouteLocationNormalizedGeneric | RouteLocationGeneric): Promise<RouteLocationNormalizedLoadedGeneric>๐ง Parameters โ
| Parameter | Type | Description |
|---|---|---|
route | RouteLocationNormalizedGeneric | RouteLocationGeneric | The resolved route object that needs to be loaded |
๐ค Returns โ
- Type:
Promise<RouteLocationNormalizedLoadedGeneric> - Description: A promise that resolves to the fully loaded route location
๐ก When to Use This Function โ
๐ Lazy Loading Scenarios โ
When working with lazy-loaded components, loadRouteLocation() ensures all dependencies are resolved:
import { loadRouteLocation } from 'vue-router'
// Ensure the route component is loaded before rendering
const loadedRoute = await loadRouteLocation(currentRoute)
// Now you can safely pass it to RouterView๐ฏ Advanced RouterView Usage โ
Use with the <RouterView> slot API for fine-grained control:
<template>
<RouterView v-slot="{ Component, route }">
<component
:is="Component"
v-if="route === loadedRoute"
/>
<LoadingSpinner v-else />
</RouterView>
</template>
<script setup>
import { ref, watch } from 'vue'
import { useRoute, loadRouteLocation } from 'vue-router'
const route = useRoute()
const loadedRoute = ref(null)
watch(route, async (newRoute) => {
loadedRoute.value = await loadRouteLocation(newRoute)
}, { immediate: true })
</script>๐ ๏ธ Practical Examples โ
Example 1: Preloading Routes โ
// Preload a specific route before navigation
async function preloadRoute(path: string) {
const route = router.resolve(path)
const loadedRoute = await loadRouteLocation(route)
// Route is now ready to be rendered
console.log('Route preloaded:', loadedRoute)
}
// Usage
preloadRoute('/dashboard')Example 2: Conditional Rendering โ
<template>
<div>
<button @click="loadAndNavigate">Go to Admin Panel</button>
<RouterView v-slot="{ Component, route }">
<component
:is="Component"
v-if="isRouteLoaded(route)"
/>
</RouterView>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { useRouter, loadRouteLocation } from 'vue-router'
const router = useRouter()
const loadedRoutes = ref(new Set())
async function loadAndNavigate() {
const route = router.resolve('/admin')
const loadedRoute = await loadRouteLocation(route)
// Mark route as loaded
loadedRoutes.value.add(loadedRoute.fullPath)
// Navigate to the preloaded route
router.push('/admin')
}
function isRouteLoaded(route) {
return loadedRoutes.value.has(route.fullPath)
}
</script>๐ Technical Details โ
๐ Loading Process โ
The function performs the following steps:
- Component Resolution - Resolves any lazy-loaded components
- Dependency Loading - Ensures all dependencies are loaded
- Route Normalization - Finalizes the route object
- Promise Resolution - Returns the fully loaded route
โก Performance Considerations โ
- Caching: Results are cached for optimal performance
- Error Handling: Proper error handling for failed loads
- Memory Management: Automatic cleanup of unused routes
๐ฏ Best Practices โ
โ Do Use When โ
- Working with complex lazy-loading scenarios
- Need fine-grained control over route rendering
- Implementing custom loading states
- Building advanced routing patterns
โ Avoid When โ
- Simple route navigation (use standard navigation instead)
- Basic applications without lazy loading
- Performance-critical paths where async loading isn't needed
๐ง Integration Examples โ
With Vue's Suspense โ
<template>
<Suspense>
<template #default>
<RouterView v-slot="{ Component, route }">
<component :is="Component" />
</RouterView>
</template>
<template #fallback>
<LoadingIndicator />
</template>
</Suspense>
</template>
<script setup>
import { Suspense } from 'vue'
import { useRoute, loadRouteLocation } from 'vue-router'
const route = useRoute()
// Preload the current route
loadRouteLocation(route)
</script>With TypeScript โ
import type { RouteLocationNormalizedLoaded } from 'vue-router'
async function safelyLoadRoute(route: any): Promise<RouteLocationNormalizedLoaded> {
try {
return await loadRouteLocation(route)
} catch (error) {
console.error('Failed to load route:', error)
throw error
}
}๐ Related Functions โ
useRoute()- Access current route informationuseRouter()- Access router instancerouter.resolve()- Resolve a route location
๐ก Pro Tip
Use loadRouteLocation() in combination with Vue's Suspense component for the best user experience when dealing with lazy-loaded routes!
Ready to optimize your route loading? Start using
loadRouteLocation()for smoother navigation experiences! ๐