You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 6, 2023. It is now read-only.
The `useRouter` composable returns the router instance and must be called in a `setup` function, plugin, or route middleware.
3
+
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.)
4
4
5
-
Within the template of a Vue component, you can access the router using `$router` and you can access router in setup() function using `useRouter()` composable.
6
-
7
-
`useRouter` provides collection of methods to help you manipulate Vue router dynamically.
8
-
9
-
```html [~/pages/*.vue]
10
-
11
-
<scriptsetup>
12
-
constrouter=useRouter();
13
-
router.push({ path:"/home" });
14
-
</script>
15
-
16
-
````
17
-
18
-
`useRouter` provides the following helper methods that we can roughly divide into four groups.
5
+
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.
@@ -32,8 +19,8 @@ Within the template of a Vue component, you can access the router using `$router
32
19
-**back:** Go back in history if possible, same as `router.go(-1)`.
33
20
-**forward:** Go forward in history if possible, same as `router.go(1)`.
34
21
-**go:** Move forward or backward through the history without the hierarchical restrictions enforced in `router.back()` and `router.forward()`.
35
-
- **push:** Programmatically navigate to a new URL by pushing an entry in the history stack.
36
-
- **replace:** Programmatically navigate to a new URL by replacing the current entry in the routes history stack.
22
+
-**push:** Programmatically navigate to a new URL by pushing an entry in the history stack.**It is recommended to use [`navigateTo`](http://v3.nuxtjs.org/api/utils/navigate-to#navigateto) instead.**
23
+
-**replace:** Programmatically navigate to a new URL by replacing the current entry in the routes history stack.**It is recommended to use [`navigateTo`](http://v3.nuxtjs.org/api/utils/navigate-to#navigateto) instead.**
37
24
38
25
> 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.
`useRouter` composable provides `afterEach`, `beforeEach` and `beforeResolve` helper methods that acts as nagivation guards.
55
42
56
-
However, Nuxt has a concept of **Route middleware** that simplifies the implementation of navigation guards and provides much better developer experience.
43
+
However, Nuxt has a concept of**route middleware** that simplifies the implementation of navigation guards and provides a better developer experience.
@@ -69,18 +56,7 @@ However, Nuxt has a concept of **Route middleware** that simplifies the implemen
69
56
70
57
## Universal router instance
71
58
72
-
Nuxt also provides a universal router instance that is different from `useRouter()`. This router instance is independent of Vue router, and provides similar helper methods as `useRouter` composable.
73
-
74
-
You can use `useNuxtApp()` composable to access this router instance.
75
-
76
-
```js [js]
77
-
const NuxtApp = useNuxtApp();
78
-
const router = NuxtApp.$router;
79
-
// router.push()
80
-
// router.onError()
81
-
// router.getRoutes()
82
-
// ...and so on
83
-
```
59
+
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`.
0 commit comments