Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 1b52dfc

Browse files
committed
docs: update universal/vue-router distinction
1 parent cc8763e commit 1b52dfc

File tree

1 file changed

+6
-30
lines changed

1 file changed

+6
-30
lines changed
Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
# `useRouter`
22

3-
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.)
44

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-
<script setup>
12-
const router = 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.
196

207
::ReadMore{link="https://router.vuejs.org/api/#currentroute"}
218
::
@@ -32,8 +19,8 @@ Within the template of a Vue component, you can access the router using `$router
3219
- **back:** Go back in history if possible, same as `router.go(-1)`.
3320
- **forward:** Go forward in history if possible, same as `router.go(1)`.
3421
- **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.**
3724

3825
> 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.
3926
@@ -53,7 +40,7 @@ router.replace({ hash: "#bio" });
5340

5441
`useRouter` composable provides `afterEach`, `beforeEach` and `beforeResolve` helper methods that acts as nagivation guards.
5542

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.
5744

5845
::ReadMore{link="/guide/directory-structure/middleware"}
5946
::
@@ -69,18 +56,7 @@ However, Nuxt has a concept of **Route middleware** that simplifies the implemen
6956

7057
## Universal router instance
7158

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`.
8460

8561
::ReadMore{link="/guide/features/routing"}
8662
::

0 commit comments

Comments
 (0)