Skip to content

Commit 1f6e110

Browse files
committed
获取官方中文 vue-router 2.0
1 parent e138d9a commit 1f6e110

23 files changed

+383
-372
lines changed

src/guide/class-and-style.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Class 与 Style 绑定
33
type: guide
44
order: 6
55
---
6+
67
数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。因为它们都是 attribute,我们可以用` v-bind` 处理它们:只需要计算出表达式最终的字符串。不过,字符串拼接麻烦又易错。因此,在 `v-bind `用于` class ``style `时,Vue.js 专门增强了它。表达式的结果类型除了字符串之外,还可以是对象或数组。
78

89

src/router/advanced/data-fetching.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ type: router
44
order: 13
55
---
66

7-
# Data Fetching
7+
# 数据获取
88

9-
Sometimes you need to fetch data from the server when a route is activated. For example, before rendering a user profile, you need to fetch the user's data from the server. We can achieve this in two different ways:
9+
有时候,进入某个路由后,需要从服务器获取数据。例如,在渲染用户信息时,你需要从服务器获取用户的数据。我们可以通过两种方式来实现:
1010

11-
- **Fetching After Navigation**: perform the navigation first, and fetch data in the incoming component's lifecycle hook. Display a loading state while data is being fetched.
11+
- **导航完成之后获取**:先完成导航,然后在接下来的组件生命周期钩子中获取数据。在数据获取期间显示『加载中』之类的指示。
1212

13-
- **Fetching Before Navigation**: Fetch data before navigation in the route enter guard, and perform the navigation after data has been fetched.
13+
- **导航完成之前获取**:导航完成前,在路由的 `enter` 钩子中获取数据,在数据获取成功后执行导航。
1414

15-
Technically, both are valid choices - it ultimately depends on the user experience you are aiming for.
15+
从技术角度讲,两种方式都不错 —— 就看你想要的用户体验是哪种。
1616

17-
## Fetching After Navigation
17+
## 导航完成后获取数据
1818

19-
When using this approach, we navigate and render the incoming component immediately, and fetch data in the component's `created` hook. It gives us the opportunity to display a loading state while the data is being fetched over the network, and we can also handle loading differently for each view.
19+
当你使用这种方式时,我们会马上导航和渲染组件,然后在组件的 `create` 钩子中获取数据。这让我们有机会在数据获取期间展示一个 loading 状态,还可以在不同视图间展示不同的 loading 状态。
2020

21-
Let's assume we have a `Post` component that needs to fetch the data for a post based on `$route.params.id`:
21+
假设我们有一个 `Post` 组件,需要基于 `$route.params.id` 获取文章数据:
2222

2323
``` html
2424
<template>
@@ -49,12 +49,12 @@ export default {
4949
}
5050
},
5151
created () {
52-
// fetch the data when the view is created and the data is
53-
// already being observed
52+
// 组件创建完后获取数据,
53+
// 此时 data 已经被 observed
5454
this.fetchData()
5555
},
5656
watch: {
57-
// call again the method if the route changes
57+
// 如果路由有变化,会再次执行该方法
5858
'$route': 'fetchData'
5959
},
6060
methods: {
@@ -75,10 +75,9 @@ export default {
7575
}
7676
```
7777

78-
## Fetching Before Navigation
78+
## 在导航完成前获取数据
7979

80-
With this approach we fetch the data before actually navigating to the new
81-
route. We can perform the data fetching in the `beforeRouteEnter` guard in the incoming component, and only call `next` when the fetch is complete:
80+
通过这种方式,我们在导航转入新的路由前获取数据。我们可以在接下来的组件的 `beforeRouteEnter` 钩子中获取数据,当数据获取成功后只调用 `next` 方法。
8281

8382
``` js
8483
export default {
@@ -89,7 +88,7 @@ export default {
8988
}
9089
},
9190
beforeRouteEnter (to, from, next) {
92-
getPost(to.params.id, (err, post) => {
91+
getPost(to.params.id, (err, post) =>
9392
if (err) {
9493
// display some global error message
9594
next(false)
@@ -100,8 +99,8 @@ export default {
10099
}
101100
})
102101
},
103-
// when route changes when this component is already rendered,
104-
// the logic will be slightly different.
102+
// 路由改变前,组件就已经渲染完了
103+
// 逻辑稍稍不同
105104
watch: {
106105
$route () {
107106
this.post = null
@@ -117,4 +116,4 @@ export default {
117116
}
118117
```
119118

120-
The user will stay on the current view while the resource is being fetched for the incoming view. It is therefore recommended to display a progress bar or some kind of indicator while the data is being fetched. If the data fetch fails, it's also necessary to display some kind of global warning message.
119+
在为后面的视图获取数据时,用户会停留在当前的界面,因此建议在数据获取期间,显示一些进度条或者别的指示。如果数据获取失败,同样有必要展示一些全局的错误提醒。

src/router/advanced/lazy-loading.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
---
2-
title: 异步加载
2+
title: 路由懒加载
33
type: router
44
order: 15
55
---
66

7-
# Lazy Loading Routes
7+
# 路由懒加载
88

9-
When building apps with a bundler, the JavaScript bundle can become quite large and thus affecting page load time. It would be more efficient if we can split each route's components into a separate chunk, and only load them when the route is visited.
9+
当打包构建应用时,Javascript 包会变得非常大,影响页面加载。如果我们能把不同路由对应的组件分割成不同的代码块,然后当路由被访问的时候才加载对应组件,这样就更加高效了。
1010

11-
Combining Vue's [async component feature](http://vuejs.org/guide/components.html#Async-Components) and Webpack's [code splitting feature](https://webpack.github.io/docs/code-splitting.html), it's trivially easy to
12-
lazy-load route components.
11+
结合 Vue 的 [异步组件](http://vuejs.org/guide/components.html#Async-Components) 和 Webpack 的 [code splitting feature](https://webpack.github.io/docs/code-splitting.html), 轻松实现路由组件的懒加载。
1312

14-
All we need to do is defining our route components as async components:
13+
我们要做的就是把路由对应的组件定义成异步组件:
1514

1615
``` js
1716
const Foo = resolve => {
18-
// require.ensure is Webpack's special syntax for a code-split point.
17+
// require.ensure 是 Webpack 的特殊语法,用来设置 code-split point
18+
// (代码分块)
1919
require.ensure(['./Foo.vue'], () => {
2020
resolve(require('./Foo.vue'))
2121
})
2222
}
2323
```
2424

25-
There's also an alternative code-split syntax using AMD style require, so this can be simplified to:
25+
这里还有另一种代码分块的语法,使用 AMD 风格的 require,于是就更简单了:
2626

2727
``` js
2828
const Foo = resolve => require(['./Foo.vue'], resolve)
2929
```
3030

31-
Nothing needs to change in the route config, just use `Foo` as usual:
31+
不需要改变任何路由配置,跟之前一样使用 `Foo`
3232

3333
``` js
3434
const router = new VueRouter({
@@ -38,14 +38,14 @@ const router = new VueRouter({
3838
})
3939
```
4040

41-
### Grouping Components in the Same Chunk
41+
### 把组件按组分块
4242

43-
Sometimes we may want to group all the components nested under the same route into the same async chunk. To achieve that we need to use [named chunks](https://webpack.github.io/docs/code-splitting.html#named-chunks) by providing a chunk name to `require.ensure` as the 3rd argument:
43+
有时候我们想把某个路由下的所有组件都打包在同个异步 chunk 中。只需要 [给 chunk 命名](https://webpack.github.io/docs/code-splitting.html#named-chunks),提供 `require.ensure` 第三个参数作为 chunk 的名称:
4444

4545
``` js
4646
const Foo = r => require.ensure([], () => r(require('./Foo.vue')), 'group-foo')
4747
const Bar = r => require.ensure([], () => r(require('./Bar.vue')), 'group-foo')
4848
const Baz = r => require.ensure([], () => r(require('./Baz.vue')), 'group-foo')
4949
```
5050

51-
Webpack will group any async module with the same chunk name into the same async chunk - this also means we don't need to explicitly list dependencies for `require.ensure` anymore (thus passing an empty array).
51+
Webpack 将相同 chunk 下的所有异步模块打包到一个异步块里面 —— 这也意味着我们无须明确列出 `require.ensure` 的依赖(传空数组就行)。

src/router/advanced/meta.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: Route Meta Fields
2+
title: 路由元信息
33
type: router
44
order: 11
55
---
66

7-
# Route Meta Fields
7+
# 路由元信息
88

9-
You can include a `meta` field when defining a route:
9+
定义路由的时候可以配置 `meta` 字段:
1010

1111
``` js
1212
const router = new VueRouter({
@@ -27,15 +27,15 @@ const router = new VueRouter({
2727
})
2828
```
2929

30-
So how do we access this `meta` field?
30+
那么如何访问这个 `meta` 字段呢?
3131

32-
First, each route object in the `routes` configuration is called a **route record**. Route records may be nested. Therefore when a route is matched, it can potentially match more than one route record.
32+
首先,我们称呼 `routes` 配置中的每个路由对象为 **路由记录**。路由记录可以是嵌套的,因此,当一个路由匹配成功后,他可能匹配多个路由记录
3333

34-
For example, with the above route config, the URL `/foo/bar` will match both the parent route record and the child route record.
34+
例如,根据上面的路由配置,`/foo/bar` 这个 URL 将会匹配父路由记录以及子路由记录。
3535

36-
All route records matched by a route are exposed on the `$route` object (and also route objects in navigation guards) as the `$route.matched` Array. Therefore, we will need to iterate over `$route.matched` to check for meta fields in route records.
36+
一个路由匹配到的所有路由记录会暴露为 `$route` 对象(还有在导航钩子中的 route 对象)的 `$route.matched` 数组。因此,我们需要遍历 `$route.matched` 来检查路由记录中的 `meta` 字段。
3737

38-
An example use case is checking for a meta field in the global navigation guard:
38+
下面例子展示在全局导航钩子中检查 meta 字段:
3939

4040
``` js
4141
router.beforeEach((to, from, next) => {
@@ -51,7 +51,7 @@ router.beforeEach((to, from, next) => {
5151
next()
5252
}
5353
} else {
54-
next() // make sure to always call next()!
54+
next() // 确保一定要调用 next()
5555
}
5656
})
5757
```
Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
---
2-
title: Navigation Guards
2+
title: 导航钩子
33
type: router
44
order: 10
55
---
66

7-
# Navigation Guards
7+
# 导航钩子
88

9-
As the name suggests, the navigation guards provided by `vue-router` are primarily used to guard navigations either by redirecting it or canceling it. There are a number of ways to hook into the route navigation process: globally, per-route, or in-component.
9+
>(译者:『导航』表示路由正在发生改变。)
1010
11-
### Global Guards
11+
正如其名,`vue-router` 提供的导航钩子主要用来拦截导航,让它完成跳转或取消。有多种方式可以在路由导航发生时执行钩子:全局的, 单个路由独享的, 或者组件级的。
1212

13-
You can register global before guards using `router.beforeEach`:
13+
### 全局钩子
14+
15+
你可以使用 `router.beforeEach` 注册一个全局的 `before` 钩子:
1416

1517
``` js
1618
const router = new VueRouter({ ... })
@@ -20,35 +22,36 @@ router.beforeEach((to, from, next) => {
2022
})
2123
```
2224

23-
Global before guards are called in creation order, whenever a navigation is triggered. Guards may be resolved asynchronously, and the navigation is considered **pending** before all hooks have been resolved.
25+
当一个导航触发时,全局的 `before` 钩子按照创建顺序调用。钩子是异步解析执行,此时导航在所有钩子 resolve 完之前一直处于 **等待中**
26+
27+
每个钩子方法接收三个参数:
2428

25-
Every guard function receives three arguments:
29+
- **`to: Route`**: 即将要进入的目标 [路由对象](../api/route-object.md)
2630

27-
- **`to: Route`**: the target [Route Object](../api/route-object.md) being navigated to.
31+
- **`from: Route`**: 当前导航正要离开的路由
2832

29-
- **`from: Route`**: the current route being navigated away from.
33+
- **`next: Function`**: 一定要调用该方法来 **resolve** 这个钩子。执行效果依赖 `next` 方法的调用参数。
3034

31-
- **`next: Function`**: this function must be called to **resolve** the hook. The action depends on the arguments provided to `next`:
35+
- **`next()`**: 进行管道中的下一个钩子。如果全部钩子执行完了,则导航的状态就是 **confirmed** (确认的)。
3236

33-
- **`next()`**: move on to the next hook in the pipeline. If no hooks are left, the navigation is **confirmed**.
37+
- **`next(false)`**: 中断当前的导航。如果浏览器的 URL 改变了(可能是用户手动或者浏览器后退按钮),那么 URL 地址会重置到 `from` 路由对应的地址。
3438

35-
- **`next(false)`**: abort the current navigation. If the browser URL was changed (either manually by the user or via back button), it will be reset to that of the `from` route.
39+
- **`next('/')` 或者 `next({ path: '/' })`**: 跳转到一个不同的地址。当前的导航被中断,然后进行一个新的导航。
3640

37-
- **`next('/')` or `next({ path: '/' })`**: redirect to a different location. The current navigation will be aborted and a new one will be started.
41+
**确保要调用 `next` 方法,否则钩子就不会被 resolved。**
3842

39-
**Make sure to always call the `next` function, otherwise the hook will never be resolved.**
4043

41-
You can also register global after hooks, however unlike guards, these hooks do not get a `next` function and cannot affect the navigation:
44+
同样可以注册一个全局的 `after` 钩子,不过它不像 `before` 钩子那样,`after` 钩子没有 `next` 方法,不能改变导航:
4245

4346
``` js
44-
router.afterEach((to, from) => {
47+
router.afterEach(route => {
4548
// ...
4649
})
4750
```
4851

49-
### Per-Route Guard
52+
### 某个路由独享的钩子
5053

51-
You can define `beforeEnter` guards directly on a route's configuration object:
54+
你可以在路由配置上直接定义 `beforeEnter` 钩子:
5255

5356
``` js
5457
const router = new VueRouter({
@@ -64,38 +67,37 @@ const router = new VueRouter({
6467
})
6568
```
6669

67-
These guards have the exact same signature as global before guards.
70+
这些钩子与全局 `before` 钩子的方法参数是一样的。
6871

69-
### In-Component Guards
72+
### 组件内的钩子
7073

71-
Finally, you can directly define route navigation guards inside route components with `beforeRouteEnter` and `beforeRouteLeave`:
74+
最后,你可以使用 `beforeRouteEnter` `beforeRouteLeave`,在路由组件内直接定义路由导航钩子,
7275

7376
``` js
7477
const Foo = {
7578
template: `...`,
76-
beforeRouteEnter (to, from, next) {
77-
// called before the route that renders this component is confirmed.
78-
// does NOT have access to `this` component instance,
79-
// because it has not been created yet when this guard is called!
79+
beforeRouteEnter (to, from, next) => {
80+
// 在渲染该组件的对应路由被 confirm 前调用
81+
// 不!能!获取组件实例 `this`
82+
// 因为当钩子执行前,组件实例还没被创建
8083
},
81-
beforeRouteLeave (to, from, next) {
82-
// called when the route that renders this component is about to
83-
// be navigated away from.
84-
// has access to `this` component instance.
84+
beforeRouteLeave (to, from, next) => {
85+
// 导航离开该组件的对应路由时调用
86+
// 可以访问组件实例 `this`
8587
}
8688
}
8789
```
8890

89-
The `beforeRouteEnter` guard does **NOT** have access to `this`, because the guard is called before the navigation is confirmed, thus the new entering component has not even been created yet.
91+
`beforeRouteEnter` 钩子 **不能** 访问 `this`,因为钩子在导航确认前被调用,因此即将登场的新组件还没被创建。
9092

91-
However, you can access the instance by passing a callback to `next`. The callback will be called when the navigation is confirmed, and the component instance will be passed to the callback as the argument:
93+
不过,你可以通过传一个回调给 `next`来访问组件实例。在导航被确认的时候执行回调,并且把组件实例作为回调方法的参数。
9294

9395
``` js
94-
beforeRouteEnter (to, from, next) {
96+
beforeRouteEnter (to, from, next) => {
9597
next(vm => {
96-
// access to component instance via `vm`
98+
// 通过 `vm` 访问组件实例
9799
})
98100
}
99101
```
100102

101-
You can directly access `this` inside `beforeRouteLeave`. The leave guard is usually used to prevent the user from accidentally leaving the route with unsaved edits. The navigation can be canceled by calling `next(false)`.
103+
你可以 在 `beforeRouteLeave` 中直接访问 `this`。这个 `leave` 钩子通常用来禁止用户在还未保存修改前突然离开。可以通过 `next(false)` 来取消导航。

0 commit comments

Comments
 (0)