Skip to content

Commit 536d449

Browse files
committed
docs: [zh-cn] custom fields
1 parent 19a3782 commit 536d449

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/zh-cn/route.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,34 @@ Vue-router 做路径匹配时支持动态片段、全匹配片段以及查询参
2121

2222
路由规则所属的路由器(以及其所属的组件)。
2323

24+
### 自定义字段
25+
26+
除了以上这些内置的属性外,在路由设置对象中的其他自定义字段也会被拷贝到最终的路由对象上。例如:
27+
28+
``` js
29+
router.map({
30+
'/a': {
31+
component: { ... },
32+
auth: true // 这里 auth 是一个自定义字段
33+
}
34+
})
35+
```
36+
37+
`/a` 被匹配时,`$route.auth` 的值将会是 `true`。我们可以利用这个特性在全局的钩子函数中进行身份验证:
38+
39+
``` js
40+
router.beforeEach(function (transition) {
41+
if (transition.to.auth) {
42+
// 对用户身份进行验证...
43+
}
44+
})
45+
```
46+
47+
当嵌套的路径被匹配时,每一个路径段的自定义字段都会被拷贝到同一个路由对象上。如果一个子路径和一个父路径有相同的字段,则子路径的值会覆盖父路径的值。
48+
2449
### 在模板中使用
2550

26-
你可以直接在组件模板中使用 $route 。例如:
51+
你可以直接在组件模板中使用 `$route` 。例如:
2752

2853
``` html
2954
<div>

0 commit comments

Comments
 (0)