This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
docs(api): add addRouteMiddleware
util
#6894
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4957702
docs(api): add addRouteMiddleware utils
Krutie d9c7daf
Update docs/content/3.api/3.utils/add-route-middleware.md
atinux 4bbdb53
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala a49d473
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala 785aca1
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala 5217461
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala e88a202
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala 7f46f4b
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala b578205
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala da83460
Update docs/content/3.api/3.utils/add-route-middleware.md
DamianGlowala 17f3522
Update add-route-middleware.md
DamianGlowala File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,7 +1,76 @@ | ||
# `addRouteMiddleware` | ||
|
||
::ReadMore{link="/guide/features/routing"} | ||
`addRouteMiddleware()` is a helper function to dynamically add route middleware in your Nuxt application. | ||
|
||
::alert{type=info} | ||
Route middleware are navigation guards stored in the [`middleware/`](/guide/directory-structure/middleware) directory of your Nuxt application (unless [set otherwise](/api/configuration/nuxt.config#middleware)). | ||
:: | ||
|
||
::NeedContribution | ||
## Usage | ||
|
||
```js | ||
addRouteMiddleware (name: string | RouteMiddleware, middleware?: RouteMiddleware, options: AddRouteMiddlewareOptions = {}) | ||
``` | ||
|
||
`addRouteMiddleware()` takes three arguments: | ||
|
||
- **name** `type: string | RouteMiddleware` | ||
|
||
`name` can be either a string or a function of type `RouteMiddleware`. Function takes the next route `to` as the first argument and the current route `from` as the second argument, both of which are Vue route objects. | ||
|
||
Learn more about available properties of [route objects](/api/composables/use-route). | ||
|
||
- **middleware**: `type: RouteMiddleware` | ||
|
||
The second argument is a function of type `RouteMiddleware`. Same as above, it provides `to` and `from` route objects. It becomes optional if the first argument in `addRouteMiddleware()` is already passed as a function. | ||
|
||
- **options:** `type: AddRouteMiddlewareOptions` | ||
|
||
An optional `options` argument lets you set the value of `global` to `true` to indicate whether the router middleware is global or not (set to `false` by default). | ||
|
||
## Examples | ||
|
||
### Anonymous Route Middleware | ||
|
||
Anonymous route middleware does not have a name. It takes a function as the first argument, making the second `middleware` argument redundant: | ||
|
||
```ts [plugins/my-plugin.ts] | ||
export default defineNuxtPlugin(() => { | ||
addRouteMiddleware((to, from) => { | ||
if (to.path === '/forbidden') { | ||
return false | ||
} | ||
}) | ||
}) | ||
``` | ||
|
||
### Named Route Middleware | ||
|
||
Named route middleware takes a string as the first argument and a function as the second. | ||
|
||
When defined in a plugin, it overrides any existing middleware of the same name located in the `/middleware` directory: | ||
|
||
```ts [plugins/my-plugin.ts] | ||
export default defineNuxtPlugin(() => { | ||
addRouteMiddleware('named-middleware', () => { | ||
console.log('named middleware added in Nuxt plugin') | ||
}) | ||
}) | ||
``` | ||
|
||
### Global Route Middleware | ||
|
||
You can set an optional, third argument `{ global: true }` to indicate whether the route middleware is global: | ||
|
||
```ts [plugins/my-plugin.ts] | ||
export default defineNuxtPlugin(() => { | ||
addRouteMiddleware('global-middleware', (to, from) => { | ||
console.log('global middleware that runs on every route change') | ||
}, | ||
{ global: true } | ||
) | ||
}) | ||
``` | ||
|
||
::ReadMore{link="/guide/features/routing"} | ||
:: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.