Nuxt/docs/content/1.docs/3.api/1.composables/use-router.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

3.1 KiB

title description
useRouter The useRouter composable returns the router instance.

useRouter

The useRouter composable returns the router instance and must be called in a setup function, plugin, or route middleware.

Within the template of a Vue component, you can access the router using $router instead.

If you have a pages/ folder, useRouter is identical in behavior to the one provided by vue-router. Feel free to read the router documentation for more information on what each method does.

::ReadMore{link="https://router.vuejs.org/api/#currentroute"} ::

Basic Manipulation

  • addRoute: Add a new route to the router instance. parentName can be provided to add new route as the child of an existing route.
  • removeRoute: Remove an existing route by its name.
  • getRoutes: Get a full list of all the route records.
  • hasRoute: Checks if a route with a given name exists.

Based on History API

  • back: Go back in history if possible, same as router.go(-1).
  • forward: Go forward in history if possible, same as router.go(1).
  • go: Move forward or backward through the history without the hierarchical restrictions enforced in router.back() and router.forward().
  • push: Programmatically navigate to a new URL by pushing an entry in the history stack. It is recommended to use navigateTo instead.
  • replace: Programmatically navigate to a new URL by replacing the current entry in the routes history stack. It is recommended to use navigateTo instead.

TIP: router.addRoute() adds route details into an array of routes and it is useful while building Nuxt plugins while router.push() on the other hand, triggers a new navigation immediately and it is useful in Nuxt Page components, Vue components and composable.

const router = useRouter();
router.back();
router.forward();
router.go();
router.push({ path: "/home" });
router.replace({ hash: "#bio" });

::ReadMore{link="https://developer.mozilla.org/en-US/docs/Web/API/History"} ::

Navigation Guards

useRouter composable provides afterEach, beforeEach and beforeResolve helper methods that acts as navigation guards.

However, Nuxt has a concept of route middleware that simplifies the implementation of navigation guards and provides a better developer experience.

::ReadMore{link="/guide/directory-structure/middleware"} ::

Promise and Error Handling

  • isReady: Returns a Promise that resolves when the router has completed the initial navigation.
  • onError: Adds an error handler that is called every time a non caught error happens during navigation.
  • resolve: Returns the normalized version of a route location. Also includes an href property that includes any existing base.

::ReadMore{link="https://router.vuejs.org/api/#router-methods"} ::

Universal Router Instance

If you do not have a pages/ folder, then useRouter will return a universal router instance with similar helper methods, but be aware that not all features may be supported or behave in exactly the same way as with vue-router.