Nuxt/docs/content/1.docs/3.api/3.utils/abort-navigation.md
Sébastien Chopin 90784f79d7
docs: new website design (#9007)
* docs: implement new website theme

* chore: rename dirs

* chore: update build

* lint fix

* chore: update deps

* fix: include node_modules in esbuild step

* chore: update deps

* Update .gitignore

* chore: update theme version

* up

* up

* fix: use svg for illustration

* chore: update to 0.0.12

* chore: force parse5 resolution

* stay with build

* feat: always display first home section

* Update yarn.lock

* chore: update theme

* fix lint

* docs: update home title

* chore: update website theme version

* Update docs/content/0.index.md

Co-authored-by: pooya parsa <pyapar@gmail.com>

* Update docs/content/0.index.md

Co-authored-by: pooya parsa <pyapar@gmail.com>

* up

* chore: bump theme version

* up

* chore: up

* up up and up

* chore: generate

* fix: boolean value

* feat: new images

* update again

* chore: up

* ouep

* chore: up

Co-authored-by: Daniel Roe <daniel@roe.dev>
Co-authored-by: Clément Ollivier <clement.o2p@gmail.com>
Co-authored-by: pooya parsa <pyapar@gmail.com>
2022-11-16 11:04:28 +01:00

1.6 KiB

title
abortNavigation

abortNavigation

abortNavigation is a helper function that prevents navigation from taking place and throws an error if one is set as a parameter.

::alert{type="warning"} abortNavigation is only usable inside a route middleware handler. ::

Type

abortNavigation(err?: Error | string): false

Parameters

err

  • Type: 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:

export default defineNuxtRouteMiddleware((to, from) => {
  const user = useState('user')

  if (!user.value.isAuthorized) {
    return abortNavigation()
  }
 
  return navigateTo('/edit-post')
})

err as a String

You can pass the error as a string:

export default defineNuxtRouteMiddleware((to, from) => {
  const auth = useState('auth')

  if (!user.value.isAuthorized) {
    abortNavigation('Insufficient permissions.')
  }
})

err as an Error Object

You can pass the error as an Error object, e.g. caught by the catch-block:

export default defineNuxtRouteMiddleware((to, from) => {
  try {
    /* code that might throw an error */
  } catch (err) {
    abortNavigation(err)
  }
})