You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you are not using auto imports, you will need to tell ESlint about `vue-router/auto` and `vue-router/auto-routes`. Add these lines to your eslint configuration:
3
+
If you are not using auto imports, you will need to tell ESlint about `vue-router/auto-routes`. Add these lines to your eslint configuration:
Copy file name to clipboardExpand all lines: docs/guide/extending-routes.md
+2-4Lines changed: 2 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,9 +42,9 @@ It's possible to override the route configuration directly in the page component
42
42
43
43
### `definePage()`
44
44
45
-
You can modify and extend any page component with the `definePage()` macro from `vue-router/auto`. This is useful for adding meta information, or modifying the route object. If you have configured auto imports, you won't need to import `definePage` from `vue-router/auto` as it is already available.
45
+
You can modify and extend any page component with the `definePage()` macro. This is useful for adding meta information, or modifying the route object. It's globally available in Vue components but you can import it from `unplugin-vue-router/runtime` if needed.
46
46
47
-
```vue{2,4-9} twoslash
47
+
```vue{2-7} twoslash
48
48
<script setup lang="ts">
49
49
// ---cut-start---
50
50
import 'unplugin-vue-router/client'
@@ -53,8 +53,6 @@ export {}
53
53
// ---cut-end---
54
54
// @errors: 2322 2339
55
55
// @moduleResolution: bundler
56
-
import { definePage } from 'unplugin-vue-router/runtime'
Copy file name to clipboardExpand all lines: docs/guide/typescript.md
+4-28Lines changed: 4 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ This plugin generates a `d.ts` file with all the typing overrides when the dev o
13
13
}
14
14
```
15
15
16
-
Then, you will be able to import from `vue-router/auto` (instead of `vue-router`) to get access to the typed APIs.
16
+
The generated _Route Map_ is picked up by `unplugin-vue-router/client` types and configures the `vue-router` types to be aware of the routes in your application. Making everything type safe!
17
17
18
18
::: tip
19
19
You can commit the newly added `.d.ts` files to your repository to make your life easier.
@@ -33,17 +33,13 @@ router.push('')
33
33
34
34
## Extra types
35
35
36
-
You can always take a look at the generated `typed-router.d.ts` file to inspect what are the generated types. `unplugin-vue-router` improves upon many of the existing types in `vue-router` and adds a few ones as well:
37
-
38
-
### `RouteNamedMap`
39
-
40
-
The `RouteNamedMap` interface gives you access to all the metadata associated with a route. It can also be extended to enable types for **dynamic routes** that are added during runtime.
36
+
You can always take a look at the generated `typed-router.d.ts` file to inspect what are the generated types. `unplugin-vue-router` creates a `RouteNamedMap` interface and exports it from `'vue-router/auto-routes'`.
This interface contains all the routes in your application along with their metadata. Augment it to add types for **dynamic routes** that are added during runtime:
The `Router` type gives you access to the typed version of the router instance. It's also the _ReturnType_ of the `useRouter()` function.
76
-
77
-
```ts
78
-
importtype { Router } from'vue-router'
79
-
```
80
-
81
-
### `RouteLocationResolved`
82
-
83
-
The `RouteLocationResolved` type exposed by `vue-router/auto` 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 }">`:
84
-
85
-
```vue
86
-
<RouterLink v-slot="{ route }">
87
-
User {{ (route as RouteLocationResolved<'/users/[id]'>).params.id }}
88
-
</RouterLink>
89
-
```
90
-
91
-
This type is also the return type of `router.resolve()`.
92
-
93
-
You have the same equivalents for `RouteLocation`, `RouteLocationNormalized`, and `RouteLocationNormalizedLoaded`. All of them exist in `vue-router` but `vue-router/auto` override them to provide a type safe version of them. In addition to that, you can pass the name of the route as a generic:
69
+
You can now pass a _type param_ to the generic route location types to narrow down the type of the route:
@@ -152,21 +162,22 @@ If you don't have an `env.d.ts` file, you can create one and add the unplugin-vu
152
162
153
163
:::
154
164
155
-
Instead of importing from `vue-router`, you should import from `vue-router/auto`.
165
+
::: warning
166
+
167
+
unplugin-vue-router will add a virtual `vue-router/auto` module that exports everything from `vue-router` with some extra features from `unplugin-vue-router/runtime`. It's recommended to avoid using `vue-router/auto` in new projects. It's kept for compatibility with existing projects that use it and will likely be removed in the future.
156
168
157
169
::: tip
158
-
You can exclude `vue-router` from VSCode import suggestions by adding this setting to your `.vscode/settings.json`:
170
+
You can exclude `vue-router/auto` from VSCode import suggestions by adding this setting to your `.vscode/settings.json`:
This will ensure VSCode only suggests`vue-router/auto` for imports. Alternatively, you can also configure [auto imports](#auto-imports).
179
+
This will ensure VSCode does not suggest`vue-router/auto` for imports. Alternatively, you can also configure [auto imports](#auto-imports).
168
180
169
-
The reason we need to import from `vue-router/auto` rather than `vue-router` is because we override all the types with typed version. It is **not possible** to override the types from `vue-router` directly as the typed ones are all generic.
170
181
:::
171
182
172
183
### Migrating an existing project
@@ -219,7 +230,7 @@ Check the [file-based routing](/guide/file-based-routing) guide for more informa
219
230
### From scratch
220
231
221
232
- Create a `src/pages` folder and add an `index.vue` component to it. This will render your home page at `/`.
222
-
-Add Vue Router using`vue-router/auto` instead of `vue-router`. These types are augmented to be fully typed.
233
+
-Import the `routes` from`vue-router/auto-routes` and pass them to the `createRouter` function.
Note that the `vue-router` preset might export less things than the one exported by `unplugin-vue-router` so you might need to add any other imports you were relying on manually:
314
-
315
-
```ts
316
-
AutoImport({
317
-
imports: [
318
-
VueRouterAutoImports,
319
-
{ // [!code ++]
320
-
// add any other imports you were relying on // [!code ++]
321
-
'vue-router/auto': ['useLink'] // [!code ++]
322
-
}, // [!code ++]
323
-
],
324
-
}),
325
-
```
326
-
327
322
If you use ESlint, check [the ESlint section](/guide/eslint).
328
323
If you use TypeScript or have a `jsconfig.json` file, check [the TypeScript section](/guide/typescript).
Copy file name to clipboardExpand all lines: docs/rfcs/data-loaders/index.md
+3-7Lines changed: 3 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,7 +203,7 @@ You might only be interested in trying out Data Loaders. In that case, check out
203
203
```ts{2,9}
204
204
import { createApp } from 'vue'
205
205
import { createRouter } from 'vue-router'
206
-
import { DataLoaderPlugin } from 'vue-router/auto'
206
+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
207
207
208
208
const router = createRouter({
209
209
// ...
@@ -564,7 +564,7 @@ Since navigation loaders can run in parallel, they can return different navigati
564
564
import 'unplugin-vue-router/client'
565
565
import { createApp } from 'vue'
566
566
import { createRouter, createWebHistory } from 'vue-router'
567
-
import { DataLoaderPlugin } from 'vue-router/auto'
567
+
import { DataLoaderPlugin } from 'unplugin-vue-router/data-loaders'
568
568
const app = createApp({})
569
569
const router = createRouter({
570
570
history: createWebHistory(),
@@ -598,7 +598,7 @@ By default, `selectNavigation` returns the first value of the array.
598
598
If a loader wants to eagerly alter the navigation, it can `throw` the `NavigationResult` instead of returning it. This skips the `selectNavigationResult()` and take precedence without triggering `router.onError()`.
599
599
600
600
```ts{10-15}
601
-
import { NavigationResult } from 'vue-router/auto'
601
+
import { NavigationResult } from 'unplugin-vue-router/data-loaders'
602
602
603
603
export const useUserData = defineLoader(
604
604
async (to) => {
@@ -830,10 +830,6 @@ This aligns with the future [Navigation API](https://github.com/WICG/navigation-
830
830
831
831
### Implementations
832
832
833
-
::: info
834
-
Ideally, we would import from `vue-router/auto`. This should be added by [unplugin-vue-router][uvr] but given the current state, we need to import from `unplugin-vue-router/data-loaders/...`.
835
-
:::
836
-
837
833
### Interfaces
838
834
839
835
Defining a minimal set of information and options for Data Loaders is what enables external libraries to implement their own data loaders. They are meant to extend these interfaces to add more features that are specific to them. You can see a practical example with the [Pinia Colada](colada.md) implementation.
0 commit comments