feat: 위키 저장소 초기 커밋
- CLAUDE.md 운영 규칙 - wiki/ 정리된 지식 페이지 (Nuxt + Claude Code) - raw/ 원본 자료 - reference/ Nuxt 4.x 공식 문서 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
73
reference/4.api/3.utils/abort-navigation.md
Normal file
73
reference/4.api/3.utils/abort-navigation.md
Normal file
@@ -0,0 +1,73 @@
|
||||
---
|
||||
title: 'abortNavigation'
|
||||
description: 'abortNavigation is a helper function that prevents navigation from taking place and throws an error if one is set as a parameter.'
|
||||
links:
|
||||
- label: Source
|
||||
icon: i-simple-icons-github
|
||||
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/router.ts
|
||||
size: xs
|
||||
---
|
||||
|
||||
::warning
|
||||
`abortNavigation` is only usable inside a [route middleware handler](/docs/4.x/directory-structure/app/middleware).
|
||||
::
|
||||
|
||||
## Type
|
||||
|
||||
```ts [Signature]
|
||||
export function abortNavigation (err?: Error | string): false
|
||||
```
|
||||
|
||||
## Parameters
|
||||
|
||||
### `err`
|
||||
|
||||
- **Type**: [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | `string`
|
||||
|
||||
Optional error to be thrown by `abortNavigation`.
|
||||
|
||||
## Examples
|
||||
|
||||
The example below shows how you can use `abortNavigation` in a route middleware to prevent unauthorized route access:
|
||||
|
||||
```ts [app/middleware/auth.ts]
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
const user = useState('user')
|
||||
|
||||
if (!user.value.isAuthorized) {
|
||||
return abortNavigation()
|
||||
}
|
||||
|
||||
if (to.path !== '/edit-post') {
|
||||
return navigateTo('/edit-post')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### `err` as a String
|
||||
|
||||
You can pass the error as a string:
|
||||
|
||||
```ts [app/middleware/auth.ts]
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
const user = useState('user')
|
||||
|
||||
if (!user.value.isAuthorized) {
|
||||
return abortNavigation('Insufficient permissions.')
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
### `err` as an Error Object
|
||||
|
||||
You can pass the error as an [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object, e.g. caught by the `catch`-block:
|
||||
|
||||
```ts [app/middleware/auth.ts]
|
||||
export default defineNuxtRouteMiddleware((to, from) => {
|
||||
try {
|
||||
/* code that might throw an error */
|
||||
} catch (err) {
|
||||
return abortNavigation(err)
|
||||
}
|
||||
})
|
||||
```
|
||||
Reference in New Issue
Block a user