Skip to content

Commit df3bcc6

Browse files
committed
docs: finish essentials
1 parent d90f633 commit df3bcc6

File tree

7 files changed

+100
-42
lines changed

7 files changed

+100
-42
lines changed

docs/en/SUMMARY.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
- [Getting Started](essentials/getting-started.md)
99
- [Dynamic Route Matching](essentials/matching.md)
1010
- [Nested Routes](essentials/nested-routes.md)
11-
- [Redirect and Alias](essentials/redirect-and-alias.md)
1211
- [Programmatic Navigation](essentials/navigation.md)
12+
- [Named Routes](essentials/named-routes.md)
13+
- [Named Views](essentials/named-views.md)
14+
- [Redirect and Alias](essentials/redirect-and-alias.md)
1315
- [Server Config for History Mode](essentials/server.md)
1416
- Advanced
15-
- [Named Routes](advanced/named-routes.md)
16-
- [Named Views](advanced/named-views.md)
1717
- [Navigation Guards](advanced/navigation-guards.md)
1818
- [Animations](advanced/animations.md)
1919
- [Data Fetching](advanced/data-fetching.md)

docs/en/advanced/named-routes.md renamed to docs/en/essentials/named-routes.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Named Routes
22

3-
Sometimes it is more convenient to identify a route with a name, especially when
4-
performing navigations. You can give a route a name in the `routes` options
5-
while creating the Router instance:
3+
Sometimes it is more convenient to identify a route with a name, especially when linking to a route or performing navigations. You can give a route a name in the `routes` options while creating the Router instance:
64

75
``` js
86
const router = new VueRouter({
97
routes: [
10-
// Foo is rendered when /foo is matched
118
{
12-
path: '/user/:userId',
9+
path: '/user/:id',
1310
name: 'user',
1411
component: User
1512
}
@@ -31,3 +28,5 @@ router.push({ name: 'user', params: { userId: 123 }})
3128
```
3229

3330
In both cases, the router will navigate to the path `/user/123`.
31+
32+
Full example [here](https://github.com/vuejs/vue-router/blob/next/examples/named-routes/app.js).

docs/en/advanced/named-views.md renamed to docs/en/essentials/named-views.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
# Named Views
22

3-
Sometimes you need to show multiples views at the same time instead of nesting
4-
them. This is where named views came in handy. Instead of having one single
5-
outlet in your view, you can have multiple and give each of them a name. A
6-
`router-view` without a name will be given `default` as its name.
3+
Sometimes you need to display multiples views at the same time instead of nesting them, e.g. creating a layout with a `sidebar` view and a `main` view. This is where named views came in handy. Instead of having one single outlet in your view, you can have multiple and give each of them a name. A `router-view` without a name will be given `default` as its name.
74

85
``` html
96
<router-view class="view one"></router-view>
107
<router-view class="view two" name="a"></router-view>
118
<router-view class="view three" name="b"></router-view>
129
```
1310

14-
A view is rendered by using a component, therefore multiple views require
15-
multiple components for the same route. Make sure to use the `components` (with
11+
A view is rendered by using a component, therefore multiple views require multiple components for the same route. Make sure to use the `components` (with
1612
an s) option:
1713

1814
``` js

docs/en/essentials/navigation.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# Programmatic Navigation
22

3-
Aside from using `<router-link>` to create anchor tags for declarative navigation, we can do this programmatically using vue-router's APIs.
3+
Aside from using `<router-link>` to create anchor tags for declarative navigation, we can do this programmatically using the router's instance methods.
44

5-
#### router.push
6-
It pushes a new entry to the history record. This is the method called internally when you click a `<router-link>`, so clicking `<router-link :to="...">` is the equivalent of calling `router.push(...)`.
5+
#### `router.push(location)`
6+
7+
To navigate to a different URL, use `router.push`. This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.
8+
9+
This is the method called internally when you click a `<router-link>`, so clicking `<router-link :to="...">` is the equivalent of calling `router.push(...)`.
710

811
| Declarative | Programmatic |
912
|-------------|--------------|
1013
| `<router-link :to="...">` | `router.push(...)` |
1114

12-
Examples:
15+
The argument can be a string path, or a location descriptor object. Examples:
16+
1317
``` js
1418
// literal string
1519
router.push('home')
@@ -20,20 +24,22 @@ router.push({ path: 'home' })
2024
// named route
2125
router.push({ name: 'user', params: { userId: 123 }})
2226

23-
// with query, resulting in /register?plan=private
27+
// with query, resulting in /register?plan=private
2428
router.push({ path: 'register', query: { plan: 'private' }})
2529
```
2630

27-
#### router.replace
28-
It acts like `router.go`, the only difference is that it navigates without creating a new history record, as its name suggests - it replaces current history record.
31+
#### `router.replace(location)`
32+
33+
It acts like `router.go`, the only difference is that it navigates without pushing a new history entry, as its name suggests - it replaces the current entry.
2934

3035
| Declarative | Programmatic |
3136
|-------------|--------------|
3237
| `<router-link :to="..." replace>` | `router.replace(...)` |
3338

3439

35-
#### router.go
36-
It takes a single integer as parameter that indicates by how many history records to go forwards or go backwards. In router's `history` and `abstract` mode, `router.go` is actually an alias to `window.history.go`, so you can read more about it [here](https://developer.mozilla.org/en-US/docs/Web/API/History).
40+
#### `router.go(n)`
41+
42+
This method takes a single integer as parameter that indicates by how many steps to go forwards or go backwards in the history stack, similar to `window.history.go(n)`.
3743

3844
Examples
3945

@@ -52,18 +58,10 @@ router.go(-100)
5258
router.go(100)
5359
```
5460

55-
5661
#### History Manipulation
5762

5863
You may have noticed that `router.push`, `router.replace` and `router.go` are counterparts of [`window.history.pushState`, `window.history.replaceState` and `window.history.go`](https://developer.mozilla.org/en-US/docs/Web/API/History), and they do imitate the `window.history` APIs.
5964

6065
Therefore, if you are already familiar with [Browser History APIs](https://developer.mozilla.org/en-US/docs/Web/API/History_API), manipulating history will be super easy with vue-router.
6166

62-
It is worth mentioning that vue-router APIs (`push`, `replace`, `go`) works consistently under all router modes (`history`, `hash` and `abstract`). Below is a table to give you a quick glance at how vue-router achieves it.
63-
64-
| Router API | Called under the hood | | |
65-
|----------------|-----------------------|-----------------------|--------------------|
66-
| | `history` mode | `hash` mode | `abstract` mode |
67-
| router.push | history.pushState | location.hash = ... | own implementation |
68-
| router.replace | history.replaceState | location.replace | own implementation |
69-
| router.go | history.go | history.go | own implementation |
67+
It is worth mentioning that vue-router navigation methods (`push`, `replace`, `go`) work consistently in all router modes (`history`, `hash` and `abstract`).

docs/en/essentials/nested-routes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ const router = new VueRouter({
7474
})
7575
```
7676

77+
**Note that nested paths that start with `/` will be treated as a root path. This allows you to leverage the component nesting without having to use a nested URL.**
78+
7779
As you can see the `children` option is just another Array of route configuration objects like `routes` itself. Therefore, you can keep nesting views as much as you need.
7880

7981
At this point, with the above configuration, when you visit `/user/foo`, nothing will be rendered inside `User`'s outlet, because no sub route is matched. Maybe you do want to render something there. In such case you can provide an empty subroute path:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Redirect and Alias
2+
3+
### Redirect
4+
5+
Redirecting is also done in the `routes` configuration. To redirect from `/a` to `/b`:
6+
7+
``` js
8+
const router = new VueRouter({
9+
routes: [
10+
{ path: '/a', redirect: '/b' }
11+
]
12+
})
13+
```
14+
15+
The redirect can also be targeting a named route:
16+
17+
``` js
18+
const router = new VueRouter({
19+
routes: [
20+
{ path: '/a', redirect: { name: 'foo' }}
21+
]
22+
})
23+
```
24+
25+
Or even use a function for dynamic redirecting:
26+
27+
``` js
28+
const router = new VueRouter({
29+
routes: [
30+
{ path: '/a', redirect: to => {
31+
// the function receives the target route as the argument
32+
// return redirect path/location here.
33+
}}
34+
]
35+
})
36+
```
37+
38+
For other advanced usage, checkout the [example](https://github.com/vuejs/vue-router/blob/next/examples/redirect/app.js).
39+
40+
### Alias
41+
42+
A redirect means when the user visits `/a`, and URL will be replaced by `/b`, and then matched as `/b`. But what is an alias?
43+
44+
**An alias of `/a` as `/b` means when the user visits `/b`, the URL remains `/b`, but it will be matched as if the user is visiting `/a`.**
45+
46+
The above can be expressed in the route configuration as:
47+
48+
``` js
49+
const router = new VueRouter({
50+
routes: [
51+
{ path: '/a', component: A, alias: '/b' }
52+
]
53+
})
54+
```
55+
56+
An alias gives you the freedom to map a UI structure to an arbitrary URL, instead of being constrained by the configuration's nesting structure.
57+
58+
For advanced usage, checkout the [example](https://github.com/vuejs/vue-router/blob/next/examples/route-alias/app.js).

docs/en/essentials/server.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
# Server Configuration for history mode
1+
# Server Configuration for History Mode
22

3-
The Router's history mode enables the use of the HTML5 pushstate adding the ability for your app to change the URL of your site without refreshing the page, and without the use of the hashbang in the URL. Using this mode however requires some pre-configuration of your server otherwise when a user visits deep linked content they'll be served a 404 Not Found. Below you can find configuration samples for several server softwares.
3+
The default mode for `vue-router` is hash mode - it uses the URL hash to simulate a full URL so that the page won't be reloaded when the URL changes.
44

5-
There are a few caveats to this, in that your server will no longer report 404 errors as all paths will serve up your index.html file. To get around this issue you should implement a catch-all route and display a 404 page within your Vue app. See the [Caveats](#caveats) section for more information.
5+
To get rid of the ugly hash, we can use the HTML5 `history.pushState` API to achieve URL navigation without page reload. But for this mode to work properly, you need to configure your server properly.
66

7-
- [Server Configurations](#server-configurations)
8-
- [Apache](#apache)
9-
- [nginx](#nginx)
10-
- [Caveats](#caveats)
7+
Because when using history mode, the URL will look like a normal url, e.g. `http://yoursite.com/user/id`. Since our app is a single page client side app, without proper server configuration the users will get a 404 if they visit that URL directly.
118

12-
## Server Configurations
9+
Therefore you need to add a catch-all fallback route to your server: if the URL doesn't match any static assets, it should serve the same `index.html` page that your app lives in.
10+
11+
## Example Server Configurations
1312

1413
#### Apache
1514

@@ -32,15 +31,21 @@ location / {
3231
}
3332
```
3433

34+
#### Node.js (Express)
35+
36+
https://github.com/bripkens/connect-history-api-fallback
37+
3538
## Caveats
3639

37-
To get around the issue that the server will no longer serve 404 errors you should implement a catch-all route within your Vue app to show a 404 page.
40+
There is a caveats to this, because that your server will no longer report 404 errors as all paths will serve up your `index.html` file. To get around the issue, you should implement a catch-all route within your Vue app to show a 404 page:
3841

3942
```javascript
40-
new VueRouter({
43+
const router = new VueRouter({
4144
mode: 'history',
4245
routes: [
4346
{ path: '*', component: NotFoundComponent }
4447
]
4548
})
4649
```
50+
51+
Alternatively, if you are using a Node.js server, you can implement the fallback by using the router on the server side to match the incoming URL, and respond with 404 if no route is matched.

0 commit comments

Comments
 (0)