# Typed Routes (v4.1.0+)
-::: danger
+::: danger ‼️ Experimental feature
- ⚠️ This feature is still experimental and will evolve in the future, make sure to follow along in release notes and check the [Troubleshooting](#troubleshooting) section if you have issues.
+ This feature is still experimental and will evolve in the future, make sure to follow along in release notes and check the [Troubleshooting](#troubleshooting) section if you have issues. This feature is very likely to be replaced by a much more efficient build-based implementation. It not recommended for projects with a lot of routes (+50) but the setup takes about a minute and can be reverted in a few seconds at any time.
:::
If you have [dynamic routes](../advanced/dynamic-routing.md), these cannot be typed and if you use [named routes](../essentials/named-routes.md), you won't be able to push to them so it's better not to use both at the same time.
+Some APIs like `useRoute()` and `router.resolve()` are still not typed while this feature is being tested.
+
## Troubleshooting
If you ever find something blocking you or making your types too slow, you can just remove the `as const` part to rollback to the previous version of the types. If something not mentioned here isn't working and you think it should be working, please open an issue on [GitHub](https://github.com/vuejs/router/issues).
path: '/with-guard/:n',
name: 'guarded',
component,
- beforeEnter(to, from, next) {
- if (to.params.n !== 'valid') next(false)
- next()
+ beforeEnter(to) {
+ if (to.params.n !== 'valid') return false
},
},
{ path: '/cant-leave', component: GuardedWithLeave },
},
})
-// router.push('/admin/dashboard')
-
declare module 'vue-router' {
export interface Config {
Router: typeof router
}
}
-declare module 'vue' {
- interface GlobalComponents {
- RouterLink: RouterLinkTyped<typeof router>
- }
-}
+// router.push({ name: 'user', params: {} })
const delay = (t: number) => new Promise(resolve => setTimeout(resolve, t))
<p>Nested level {{ level }}</p>
<ul v-if="level === 1 && $route.name === 'Nested'">
<li>
- <router-link to="/nested/nested">/nested/nested</router-link>
+ <RouterLink to="/nested/nested">/nested/nested</RouterLink>
</li>
<li>
<router-link to="/anidado/nested">/anidado/nested</router-link>
createRouter,
createWebHistory,
useRoute,
+ loadRouteLocation,
} from 'vue-router'
import {
createApp,
await router.push({
name: 'user',
params: { id },
- // state: { backgroundView },
+ state: { backgroundView },
})
-
- history.replaceState({ ...history.state, backgroundView }, '')
- historyState.value = history.state
}
function closeUserModal() {
setup() {
const modal = ref<HTMLDialogElement | HTMLElement>()
const route = useRoute()
- // const historyState = computed(() => route.fullPath && window.history.state)
const userId = computed(() => route.params.id)
historyState.value = history.state
})
-router.beforeEach((to, from, next) => {
+router.beforeEach((to, from) => {
console.log('---')
console.log('going from', from.fullPath, 'to', to.fullPath)
console.log('state:', window.history.state)
console.log('---')
- next()
+})
+
+router.beforeResolve(async to => {
+ if (historyState.value && historyState.value.backgroundView) {
+ await loadRouteLocation(router.resolve(historyState.value.backgroundView))
+ }
})
// avoid navigating to non existent users
const app = createApp({
setup() {
const route = useRoute()
- // const historyState = computed(() => route.fullPath && window.history.state)
+
const routeWithModal = computed(() => {
if (historyState.value.backgroundView) {
return router.resolve(
}
})
- return { route, routeWithModal, historyState, ...toRefs(route) }
+ return { routeWithModal }
},
template: `
}
/**
- * Router instance
+ * Router instance. **The `Options` generic is internal**.
*/
export interface Router<Options extends RouterOptions = RouterOptions> {
/**
/**
* Called automatically by `app.use(router)`. Should not be called manually by
- * the user.
+ * the user. This will trigger the initial navigation when on client side.
*
* @internal
* @param app - Application that uses the router