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.
parentName
can be provided to add new route as the child of an existing route.router.go(-1)
.router.go(1)
.router.back()
and router.forward()
.navigateTo
instead.navigateTo
instead.TIP:
router.addRoute()
adds route details into an array of routes and it is useful while building Nuxt plugins whilerouter.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" });
useRouter
composable provides afterEach
, beforeEach
and beforeResolve
helper methods that acts as nagivation guards.
However, Nuxt has a concept of route middleware that simplifies the implementation of navigation guards and provides a better developer experience.
href
property that includes any existing base.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
.