Skip to content

Commit 039bd8f

Browse files
Jinjiangposva
authored andcommitted
docs(zh): updated (#2903)
* docs(zh): updated * Apply suggestions from code review Co-Authored-By: GU Yiling <[email protected]> * Update README.md * Update README.md
1 parent f40139c commit 039bd8f

File tree

3 files changed

+102
-62
lines changed

3 files changed

+102
-62
lines changed

Diff for: docs/zh/api/README.md

+99-59
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,52 @@ sidebar: auto
1414
`<router-link>` 比起写死的 `<a href="...">` 会好一些,理由如下:
1515

1616
- 无论是 HTML5 history 模式还是 hash 模式,它的表现行为一致,所以,当你要切换路由模式,或者在 IE9 降级使用 hash 模式,无须作任何变动。
17-
1817
- 在 HTML5 history 模式下,`router-link` 会守卫点击事件,让浏览器不再重新加载页面。
19-
2018
- 当你在 HTML5 history 模式下使用 `base` 选项之后,所有的 `to` 属性都不需要写 (基路径) 了。
2119

22-
### 将激活 class 应用在外层元素
20+
### `v-slot` API (3.1.0 新增)
21+
22+
`router-link` 通过一个[作用域插槽](https://cn.vuejs.org/v2/guide/components-slots.html#作用域插槽)暴露底层的定制能力。这是一个更高阶的 API,主要面向库作者,但也可以为开发者提供便利,多数情况用在一个类似 _NavLink_ 这样的组件里。
2323

24-
有时候我们要让激活 class 应用在外层元素,而不是 `<a>` 标签本身,那么可以用 `<router-link>` 渲染外层元素,包裹着内层的原生 `<a>` 标签:
24+
**在使用 `v-slot` API 时,需要向 `router-link` 传入一个单独的子元素**。否则 `router-link` 将会把子元素包裹在一个 `span` 元素内。
25+
26+
```html
27+
<router-link
28+
to="/about"
29+
v-slot="{ href, route, navigate, isActive, isExactActive }"
30+
>
31+
<NavLink :active="isActive" :href="href" @click="navigate"
32+
>{{ route.fullPath }}</NavLink
33+
>
34+
</router-link>
35+
```
2536

26-
``` html
27-
<router-link tag="li" to="/foo">
28-
<a>/foo</a>
37+
- `href`:解析后的 URL。将会作为一个 `a` 元素的 `href` attribute。
38+
- `route`:解析后的规范化的地址。
39+
- `navigate`:触发导航的函数。**会在必要时自动阻止事件**,和 `router-link` 同理。
40+
- `isActive`:如果需要应用[激活的 class](#active-class) 则为 `true`。允许应用一个任意的 class。
41+
- `isExactActive`:如果需要应用[精确激活的 class](#exact-active-class) 则为 `true`。允许应用一个任意的 class。
42+
43+
### 示例:将激活的 class 应用在外层元素
44+
45+
有的时候我们可能想把激活的 class 应用到一个外部元素而不是 `<a>` 标签本身,这时你可以在一个 `router-link` 中包裹该元素并使用 `v-slot` property 来创建链接:
46+
47+
```html
48+
<router-link
49+
to="/foo"
50+
v-slot="{ href, route, navigate, isActive, isExactActive }"
51+
>
52+
<li
53+
:class="[isActive && 'router-link-active', isExactActive && 'router-link-exact-active']"
54+
>
55+
<a :href="href" @click="navigate">{{ route.fullPath }}</a>
56+
</li>
2957
</router-link>
3058
```
3159

32-
在这种情况下,`<a>` 将作为真实的链接 (它会获得正确的 `href` 的),而 "激活时的 CSS 类名" 则设置到外层的 `<li>`
60+
:::tip 提示
61+
如果你在 `<a>` 元素上添加一个 `target="_blank"`,则 `@click="navigate"` 处理器会被忽略。
62+
:::
3363

3464
## `<router-link>` Props
3565

@@ -40,7 +70,7 @@ sidebar: auto
4070

4171
表示目标路由的链接。当被点击后,内部会立刻把 `to` 的值传到 `router.push()`,所以这个值可以是一个字符串或者是描述目标位置的对象。
4272

43-
``` html
73+
```html
4474
<!-- 字符串 -->
4575
<router-link to="home">Home</router-link>
4676
<!-- 渲染结果 -->
@@ -59,7 +89,9 @@ sidebar: auto
5989
<router-link :to="{ name: 'user', params: { userId: 123 }}">User</router-link>
6090

6191
<!-- 带查询参数,下面的结果为 /register?plan=private -->
62-
<router-link :to="{ path: 'register', query: { plan: 'private' }}">Register</router-link>
92+
<router-link :to="{ path: 'register', query: { plan: 'private' }}"
93+
>Register</router-link
94+
>
6395
```
6496

6597
### replace
@@ -69,7 +101,7 @@ sidebar: auto
69101

70102
设置 `replace` 属性的话,当点击时,会调用 `router.replace()` 而不是 `router.push()`,于是导航后不会留下 history 记录。
71103

72-
``` html
104+
```html
73105
<router-link :to="{ path: '/abc'}" replace></router-link>
74106
```
75107

@@ -80,7 +112,7 @@ sidebar: auto
80112

81113
设置 `append` 属性后,则在当前 (相对) 路径前添加基路径。例如,我们从 `/a` 导航到一个相对路径 `b`,如果没有配置 `append`,则路径为 `/b`,如果配了,则为 `/a/b`
82114

83-
``` html
115+
```html
84116
<router-link :to="{ path: 'relative/path'}" append></router-link>
85117
```
86118

@@ -92,7 +124,7 @@ sidebar: auto
92124
有时候想要 `<router-link>` 渲染成某种标签,例如 `<li>`
93125
于是我们使用 `tag` prop 类指定何种标签,同样它还是会监听点击,触发导航。
94126

95-
``` html
127+
```html
96128
<router-link to="/foo" tag="li">foo</router-link>
97129
<!-- 渲染结果 -->
98130
<li>foo</li>
@@ -103,21 +135,21 @@ sidebar: auto
103135
- 类型: `string`
104136
- 默认值: `"router-link-active"`
105137

106-
设置 链接激活时使用的 CSS 类名。默认值可以通过路由的构造选项 `linkActiveClass` 来全局配置。
138+
设置链接激活时使用的 CSS 类名。默认值可以通过路由的构造选项 `linkActiveClass` 来全局配置。
107139

108140
### exact
109141

110142
- 类型: `boolean`
111143
- 默认值: `false`
112144

113-
"是否激活" 默认类名的依据是 **inclusive match** (全包含匹配)
145+
是否激活默认类名的依据是**包含匹配**
114146
举个例子,如果当前的路径是 `/a` 开头的,那么 `<router-link to="/a">` 也会被设置 CSS 类名。
115147

116-
按照这个规则,每个路由都会激活`<router-link to="/">`!想要链接使用 "exact 匹配模式",则使用 `exact` 属性:
148+
按照这个规则,每个路由都会激活 `<router-link to="/">`!想要链接使用“精确匹配模式”,则使用 `exact` 属性:
117149

118-
``` html
150+
```html
119151
<!-- 这个链接只会在地址为 / 的时候被激活 -->
120-
<router-link to="/" exact>
152+
<router-link to="/" exact></router-link>
121153
```
122154

123155
查看更多关于激活链接类名的例子[可运行](https://jsfiddle.net/8xrk1n9f/)
@@ -145,7 +177,7 @@ sidebar: auto
145177

146178
因为它也是个组件,所以可以配合 `<transition>``<keep-alive>` 使用。如果两个结合一起用,要确保在内层使用 `<keep-alive>`
147179

148-
``` html
180+
```html
149181
<transition>
150182
<keep-alive>
151183
<router-view></router-view>
@@ -170,22 +202,22 @@ sidebar: auto
170202

171203
`RouteConfig` 的类型定义:
172204

173-
``` js
174-
declare type RouteConfig = {
175-
path: string;
176-
component?: Component;
177-
name?: string; // 命名路由
178-
components?: { [name: string]: Component }; // 命名视图组件
179-
redirect?: string | Location | Function;
180-
props?: boolean | Object | Function;
181-
alias?: string | Array<string>;
182-
children?: Array<RouteConfig>; // 嵌套路由
183-
beforeEnter?: (to: Route, from: Route, next: Function) => void;
184-
meta?: any;
205+
```ts
206+
interface RouteConfig = {
207+
path: string,
208+
component?: Component,
209+
name?: string, // 命名路由
210+
components?: { [name: string]: Component }, // 命名视图组件
211+
redirect?: string | Location | Function,
212+
props?: boolean | Object | Function,
213+
alias?: string | Array<string>,
214+
children?: Array<RouteConfig>, // 嵌套路由
215+
beforeEnter?: (to: Route, from: Route, next: Function) => void,
216+
meta?: any,
185217

186218
// 2.6.0+
187-
caseSensitive?: boolean; // 匹配规则是否大小写敏感?(默认值:false)
188-
pathToRegexpOptions?: Object; // 编译正则的选项
219+
caseSensitive?: boolean, // 匹配规则是否大小写敏感?(默认值:false)
220+
pathToRegexpOptions?: Object // 编译正则的选项
189221
}
190222
```
191223

@@ -217,15 +249,15 @@ sidebar: auto
217249

218250
- 默认值: `"router-link-active"`
219251

220-
全局配置 `<router-link>` 的默认“激活 class 类名”。参考 [router-link](#router-link)
252+
全局配置 `<router-link>` 默认的激活的 class。参考 [router-link](#router-link)
221253

222254
### linkExactActiveClass
223255

224256
- 类型: `string`
225257

226258
- 默认值: `"router-link-exact-active"`
227259

228-
全局配置 `<router-link>` 精确激活的默认的 class。可同时翻阅 [router-link](#router-link)
260+
全局配置 `<router-link>` 默认的精确激活的 class。可同时翻阅 [router-link](#router-link)
229261

230262
### scrollBehavior
231263

@@ -285,18 +317,20 @@ sidebar: auto
285317
## Router 实例方法
286318

287319
### router.beforeEach
320+
288321
### router.beforeResolve
322+
289323
### router.afterEach
290324

291325
函数签名:
292326

293-
``` js
327+
```js
294328
router.beforeEach((to, from, next) => {
295-
/* must call `next` */
329+
/* 必须调用 `next` */
296330
})
297331

298332
router.beforeResolve((to, from, next) => {
299-
/* must call `next` */
333+
/* 必须调用 `next` */
300334
})
301335

302336
router.afterEach((to, from) => {})
@@ -307,14 +341,18 @@ router.afterEach((to, from) => {})
307341
在 2.5.0+ 这三个方法都返回一个移除已注册的守卫/钩子的函数。
308342

309343
### router.push
344+
310345
### router.replace
346+
311347
### router.go
348+
312349
### router.back
350+
313351
### router.forward
314352

315353
函数签名:
316354

317-
``` js
355+
```js
318356
router.push(location, onComplete?, onAbort?)
319357
router.push(location).then(onComplete).catch(onAbort)
320358
router.replace(location, onComplete?, onAbort?)
@@ -330,7 +368,7 @@ router.forward()
330368
331369
函数签名:
332370
333-
``` js
371+
```js
334372
const matchedComponents: Array<Component> = router.getMatchedComponents(location?)
335373
```
336374
@@ -340,7 +378,7 @@ const matchedComponents: Array<Component> = router.getMatchedComponents(location
340378
341379
函数签名:
342380
343-
``` js
381+
```js
344382
const resolved: {
345383
location: Location;
346384
route: Route;
@@ -357,7 +395,7 @@ const resolved: {
357395
358396
函数签名:
359397
360-
``` js
398+
```js
361399
router.addRoutes(routes: Array<RouteConfig>)
362400
```
363401
@@ -367,7 +405,7 @@ router.addRoutes(routes: Array<RouteConfig>)
367405
368406
函数签名:
369407
370-
``` js
408+
```js
371409
router.onReady(callback, [errorCallback])
372410
```
373411
@@ -381,7 +419,7 @@ router.onReady(callback, [errorCallback])
381419
382420
函数签名:
383421
384-
``` js
422+
```js
385423
router.onError(callback)
386424
```
387425
@@ -409,65 +447,67 @@ router.onError(callback)
409447
410448
- 导航守卫的参数:
411449
412-
``` js
450+
```js
413451
router.beforeEach((to, from, next) => {
414452
// `to` 和 `from` 都是路由对象
415453
})
416454
```
417455
418456
- `scrollBehavior` 方法的参数:
419457
420-
``` js
458+
```js
421459
const router = new VueRouter({
422-
scrollBehavior (to, from, savedPosition) {
460+
scrollBehavior(to, from, savedPosition) {
423461
// `to` 和 `from` 都是路由对象
424462
}
425463
})
426464
```
427465
428466
### 路由对象属性
429467
430-
- **$route.path**
468+
- **\$route.path**
431469
432470
- 类型: `string`
433471
434472
字符串,对应当前路由的路径,总是解析为绝对路径,如 `"/foo/bar"`
435473
436-
- **$route.params**
474+
- **\$route.params**
437475
438476
- 类型: `Object`
439477
440478
一个 key/value 对象,包含了动态片段和全匹配片段,如果没有路由参数,就是一个空对象。
441479
442-
- **$route.query**
480+
- **\$route.query**
443481
444482
- 类型: `Object`
445483
446484
一个 key/value 对象,表示 URL 查询参数。例如,对于路径 `/foo?user=1`,则有 `$route.query.user == 1`,如果没有查询参数,则是个空对象。
447485
448-
- **$route.hash**
486+
- **\$route.hash**
449487
450488
- 类型: `string`
451489
452490
当前路由的 hash 值 (带 `#`) ,如果没有 hash 值,则为空字符串。
453491
454-
- **$route.fullPath**
492+
- **\$route.fullPath**
455493
456494
- 类型: `string`
457495
458496
完成解析后的 URL,包含查询参数和 hash 的完整路径。
459497
460-
- **$route.matched**
498+
- **\$route.matched**
461499
462500
- 类型: `Array<RouteRecord>`
463501
464502
一个数组,包含当前路由的所有嵌套路径片段的**路由记录** 。路由记录就是 `routes` 配置数组中的对象副本 (还有在 `children` 数组)。
465503
466-
``` js
504+
```js
467505
const router = new VueRouter({
468506
routes: [
469507
// 下面的对象就是路由记录
470-
{ path: '/foo', component: Foo,
508+
{
509+
path: '/foo',
510+
component: Foo,
471511
children: [
472512
// 这也是个路由记录
473513
{ path: 'bar', component: Bar }
@@ -479,11 +519,11 @@ router.onError(callback)
479519
480520
当 URL 为 `/foo/bar``$route.matched` 将会是一个包含从上到下的所有对象 (副本)。
481521
482-
- **$route.name**
522+
- **\$route.name**
483523
484524
当前路由的名称,如果有的话。(查看[命名路由](../guide/essentials/named-routes.md))
485525
486-
- **$route.redirectedFrom**
526+
- **\$route.redirectedFrom**
487527
488528
如果存在重定向,即为重定向来源的路由的名字。(参阅[重定向和别名](../guide/essentials/redirect-and-alias.md))
489529
@@ -493,11 +533,11 @@ router.onError(callback)
493533
494534
通过在 Vue 根实例的 `router` 配置传入 router 实例,下面这些属性成员会被注入到每个子组件。
495535
496-
- **this.$router**
536+
- **this.\$router**
497537
498538
router 实例。
499539
500-
- **this.$route**
540+
- **this.\$route**
501541
502542
当前激活的[路由信息对象](#路由对象)。这个属性是只读的,里面的属性是 immutable (不可变) 的,不过你可以 watch (监测变化) 它。
503543

0 commit comments

Comments
 (0)