-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types): expose some useful route location types
- Loading branch information
Showing
6 changed files
with
105 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,6 +325,27 @@ The `RouterTyped` type gives you access to the typed version of the router insta | |
import type { RouterTyped } from '@vue-router' | ||
``` | ||
|
||
#### `RouterLocationResolved` | ||
|
||
The `RouterLocationResolved` type exposed by `@vue-router` allows passing a generic (which autocomplete) to type a route **whenever checking the name doesn't makes sense because you know the type**. This is useful for cases like `<RouterLink v-slot="{ route }">`: | ||
|
||
```vue | ||
<RouterLink v-slot="{ route }"> | ||
User {{ (route as RouterLocationResolved<'/users/[id]'>).params.id }} | ||
</RouterLink> | ||
``` | ||
|
||
This type corresponds to the return type of `router.resolve()`. | ||
|
||
You have the same equivalents for `RouterLocation`, `RouterLocationNormalized`, and `RouterLocationNormalizedLoaded`. All of them exist in `vue-router` but the one exposed by `@vue-router` accept a generic: | ||
|
||
```ts | ||
// these are all valid | ||
let userWithId: RouterLocationNormalizedLoaded<'/users/[id]'> = useRoute() | ||
userWithId = useRoute<'/users/[id]'>() | ||
userWithId = useRoute('/users/[id]') // 👈 this one is the easiest to write | ||
``` | ||
|
||
## Named views | ||
|
||
It is possible to define [named views](https://router.vuejs.org/guide/essentials/named-views.html#named-views) by appending an `@` + a name to their filename, e.g. a file named `src/routes/[email protected]` will generate a route of: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters