@@ -14,22 +14,52 @@ sidebar: auto
14
14
` <router-link> ` 比起写死的 ` <a href="..."> ` 会好一些,理由如下:
15
15
16
16
- 无论是 HTML5 history 模式还是 hash 模式,它的表现行为一致,所以,当你要切换路由模式,或者在 IE9 降级使用 hash 模式,无须作任何变动。
17
-
18
17
- 在 HTML5 history 模式下,` router-link ` 会守卫点击事件,让浏览器不再重新加载页面。
19
-
20
18
- 当你在 HTML5 history 模式下使用 ` base ` 选项之后,所有的 ` to ` 属性都不需要写 (基路径) 了。
21
19
22
- ### 将激活 class 应用在外层元素
20
+ ### ` v-slot ` API (3.1.0 新增)
21
+
22
+ ` router-link ` 通过一个[ 作用域插槽] ( https://cn.vuejs.org/v2/guide/components-slots.html#作用域插槽 ) 暴露底层的定制能力。这是一个更高阶的 API,主要面向库作者,但也可以为开发者提供便利,多数情况用在一个类似 _ NavLink_ 这样的组件里。
23
23
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
+ ```
25
36
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 >
29
57
</router-link >
30
58
```
31
59
32
- 在这种情况下,` <a> ` 将作为真实的链接 (它会获得正确的 ` href ` 的),而 "激活时的 CSS 类名" 则设置到外层的 ` <li> ` 。
60
+ :::tip 提示
61
+ 如果你在 ` <a> ` 元素上添加一个 ` target="_blank" ` ,则 ` @click="navigate" ` 处理器会被忽略。
62
+ :::
33
63
34
64
## ` <router-link> ` Props
35
65
@@ -40,7 +70,7 @@ sidebar: auto
40
70
41
71
表示目标路由的链接。当被点击后,内部会立刻把 ` to ` 的值传到 ` router.push() ` ,所以这个值可以是一个字符串或者是描述目标位置的对象。
42
72
43
- ``` html
73
+ ``` html
44
74
<!-- 字符串 -->
45
75
<router-link to =" home" >Home</router-link >
46
76
<!-- 渲染结果 -->
@@ -59,7 +89,9 @@ sidebar: auto
59
89
<router-link :to =" { name: 'user', params: { userId: 123 }}" >User</router-link >
60
90
61
91
<!-- 带查询参数,下面的结果为 /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
+ >
63
95
```
64
96
65
97
### replace
@@ -69,7 +101,7 @@ sidebar: auto
69
101
70
102
设置 ` replace ` 属性的话,当点击时,会调用 ` router.replace() ` 而不是 ` router.push() ` ,于是导航后不会留下 history 记录。
71
103
72
- ``` html
104
+ ``` html
73
105
<router-link :to =" { path: '/abc'}" replace ></router-link >
74
106
```
75
107
@@ -80,7 +112,7 @@ sidebar: auto
80
112
81
113
设置 ` append ` 属性后,则在当前 (相对) 路径前添加基路径。例如,我们从 ` /a ` 导航到一个相对路径 ` b ` ,如果没有配置 ` append ` ,则路径为 ` /b ` ,如果配了,则为 ` /a/b `
82
114
83
- ``` html
115
+ ``` html
84
116
<router-link :to =" { path: 'relative/path'}" append ></router-link >
85
117
```
86
118
@@ -92,7 +124,7 @@ sidebar: auto
92
124
有时候想要 ` <router-link> ` 渲染成某种标签,例如 ` <li> ` 。
93
125
于是我们使用 ` tag ` prop 类指定何种标签,同样它还是会监听点击,触发导航。
94
126
95
- ``` html
127
+ ``` html
96
128
<router-link to =" /foo" tag =" li" >foo</router-link >
97
129
<!-- 渲染结果 -->
98
130
<li >foo</li >
@@ -103,21 +135,21 @@ sidebar: auto
103
135
- 类型: ` string `
104
136
- 默认值: ` "router-link-active" `
105
137
106
- 设置 链接激活时使用的 CSS 类名。默认值可以通过路由的构造选项 ` linkActiveClass ` 来全局配置。
138
+ 设置链接激活时使用的 CSS 类名。默认值可以通过路由的构造选项 ` linkActiveClass ` 来全局配置。
107
139
108
140
### exact
109
141
110
142
- 类型: ` boolean `
111
143
- 默认值: ` false `
112
144
113
- " 是否激活" 默认类名的依据是 ** inclusive match ** (全包含匹配) 。
145
+ “ 是否激活” 默认类名的依据是** 包含匹配 ** 。
114
146
举个例子,如果当前的路径是 ` /a ` 开头的,那么 ` <router-link to="/a"> ` 也会被设置 CSS 类名。
115
147
116
- 按照这个规则,每个路由都会激活` <router-link to="/"> ` !想要链接使用 "exact 匹配模式" ,则使用 ` exact ` 属性:
148
+ 按照这个规则,每个路由都会激活 ` <router-link to="/"> ` !想要链接使用“精确匹配模式” ,则使用 ` exact ` 属性:
117
149
118
- ``` html
150
+ ``` html
119
151
<!-- 这个链接只会在地址为 / 的时候被激活 -->
120
- <router-link to =" /" exact >
152
+ <router-link to =" /" exact ></ router-link >
121
153
```
122
154
123
155
查看更多关于激活链接类名的例子[ 可运行] ( https://jsfiddle.net/8xrk1n9f/ )
@@ -145,7 +177,7 @@ sidebar: auto
145
177
146
178
因为它也是个组件,所以可以配合 ` <transition> ` 和 ` <keep-alive> ` 使用。如果两个结合一起用,要确保在内层使用 ` <keep-alive> ` :
147
179
148
- ``` html
180
+ ``` html
149
181
<transition >
150
182
<keep-alive >
151
183
<router-view ></router-view >
@@ -170,22 +202,22 @@ sidebar: auto
170
202
171
203
` RouteConfig ` 的类型定义:
172
204
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 ,
185
217
186
218
// 2.6.0+
187
- caseSensitive?: boolean; // 匹配规则是否大小写敏感?(默认值:false)
188
- pathToRegexpOptions?: Object ; // 编译正则的选项
219
+ caseSensitive? : boolean , // 匹配规则是否大小写敏感?(默认值:false)
220
+ pathToRegexpOptions? : Object // 编译正则的选项
189
221
}
190
222
```
191
223
@@ -217,15 +249,15 @@ sidebar: auto
217
249
218
250
- 默认值: ` "router-link-active" `
219
251
220
- 全局配置 ` <router-link> ` 的默认“激活 class 类名” 。参考 [ router-link] ( #router-link ) 。
252
+ 全局配置 ` <router-link> ` 默认的激活的 class。参考 [ router-link] ( #router-link ) 。
221
253
222
254
### linkExactActiveClass
223
255
224
256
- 类型: ` string `
225
257
226
258
- 默认值: ` "router-link-exact-active" `
227
259
228
- 全局配置 ` <router-link> ` 精确激活的默认的 class。可同时翻阅 [ router-link] ( #router-link ) 。
260
+ 全局配置 ` <router-link> ` 默认的精确激活的 class。可同时翻阅 [ router-link] ( #router-link ) 。
229
261
230
262
### scrollBehavior
231
263
@@ -285,18 +317,20 @@ sidebar: auto
285
317
## Router 实例方法
286
318
287
319
### router.beforeEach
320
+
288
321
### router.beforeResolve
322
+
289
323
### router.afterEach
290
324
291
325
函数签名:
292
326
293
- ``` js
327
+ ``` js
294
328
router .beforeEach ((to , from , next ) => {
295
- /* must call `next` */
329
+ /* 必须调用 `next` */
296
330
})
297
331
298
332
router .beforeResolve ((to , from , next ) => {
299
- /* must call `next` */
333
+ /* 必须调用 `next` */
300
334
})
301
335
302
336
router .afterEach ((to , from ) => {})
@@ -307,14 +341,18 @@ router.afterEach((to, from) => {})
307
341
在 2.5.0+ 这三个方法都返回一个移除已注册的守卫/钩子的函数。
308
342
309
343
### router.push
344
+
310
345
### router.replace
346
+
311
347
### router.go
348
+
312
349
### router.back
350
+
313
351
### router.forward
314
352
315
353
函数签名:
316
354
317
- ``` js
355
+ ``` js
318
356
router .push (location, onComplete? , onAbort? )
319
357
router .push (location).then (onComplete).catch (onAbort)
320
358
router .replace (location, onComplete? , onAbort? )
@@ -330,7 +368,7 @@ router.forward()
330
368
331
369
函数签名:
332
370
333
- ` ` ` js
371
+ ` ` ` js
334
372
const matchedComponents: Array <Component > = router .getMatchedComponents (location? )
335
373
` ` `
336
374
@@ -340,7 +378,7 @@ const matchedComponents: Array<Component> = router.getMatchedComponents(location
340
378
341
379
函数签名:
342
380
343
- ` ` ` js
381
+ ` ` ` js
344
382
const resolved: {
345
383
location: Location ;
346
384
route: Route;
@@ -357,7 +395,7 @@ const resolved: {
357
395
358
396
函数签名:
359
397
360
- ` ` ` js
398
+ ` ` ` js
361
399
router .addRoutes (routes: Array < RouteConfig> )
362
400
` ` `
363
401
@@ -367,7 +405,7 @@ router.addRoutes(routes: Array<RouteConfig>)
367
405
368
406
函数签名:
369
407
370
- ` ` ` js
408
+ ` ` ` js
371
409
router .onReady (callback, [errorCallback])
372
410
` ` `
373
411
@@ -381,7 +419,7 @@ router.onReady(callback, [errorCallback])
381
419
382
420
函数签名:
383
421
384
- ` ` ` js
422
+ ` ` ` js
385
423
router .onError (callback)
386
424
` ` `
387
425
@@ -409,65 +447,67 @@ router.onError(callback)
409
447
410
448
- 导航守卫的参数:
411
449
412
- ` ` ` js
450
+ ` ` ` js
413
451
router .beforeEach ((to , from , next ) => {
414
452
// `to` 和 `from` 都是路由对象
415
453
})
416
454
` ` `
417
455
418
456
- ` scrollBehavior` 方法的参数:
419
457
420
- ` ` ` js
458
+ ` ` ` js
421
459
const router = new VueRouter ({
422
- scrollBehavior (to , from , savedPosition ) {
460
+ scrollBehavior (to , from , savedPosition ) {
423
461
// `to` 和 `from` 都是路由对象
424
462
}
425
463
})
426
464
` ` `
427
465
428
466
### 路由对象属性
429
467
430
- - **$route.path**
468
+ - **\ $ route.path**
431
469
432
470
- 类型: ` string`
433
471
434
472
字符串,对应当前路由的路径,总是解析为绝对路径,如 ` " /foo/bar" ` 。
435
473
436
- - **$route.params**
474
+ - **\ $ route.params**
437
475
438
476
- 类型: ` Object `
439
477
440
478
一个 key/value 对象,包含了动态片段和全匹配片段,如果没有路由参数,就是一个空对象。
441
479
442
- - **$route.query**
480
+ - **\ $ route.query**
443
481
444
482
- 类型: ` Object `
445
483
446
484
一个 key/value 对象,表示 URL 查询参数。例如,对于路径 ` / foo? user= 1 ` ,则有 ` $route .query .user == 1 ` ,如果没有查询参数,则是个空对象。
447
485
448
- - **$route.hash**
486
+ - **\ $ route.hash**
449
487
450
488
- 类型: ` string`
451
489
452
490
当前路由的 hash 值 (带 ` #` ) ,如果没有 hash 值,则为空字符串。
453
491
454
- - **$route.fullPath**
492
+ - **\ $ route.fullPath**
455
493
456
494
- 类型: ` string`
457
495
458
496
完成解析后的 URL,包含查询参数和 hash 的完整路径。
459
497
460
- - **$route.matched**
498
+ - **\ $ route.matched**
461
499
462
500
- 类型: ` Array < RouteRecord> `
463
501
464
502
一个数组,包含当前路由的所有嵌套路径片段的**路由记录** 。路由记录就是 ` routes` 配置数组中的对象副本 (还有在 ` children` 数组)。
465
503
466
- ` ` ` js
504
+ ` ` ` js
467
505
const router = new VueRouter ({
468
506
routes: [
469
507
// 下面的对象就是路由记录
470
- { path: ' /foo' , component: Foo,
508
+ {
509
+ path: ' /foo' ,
510
+ component: Foo,
471
511
children: [
472
512
// 这也是个路由记录
473
513
{ path: ' bar' , component: Bar }
@@ -479,11 +519,11 @@ router.onError(callback)
479
519
480
520
当 URL 为 ` / foo/ bar` ,` $route .matched ` 将会是一个包含从上到下的所有对象 (副本)。
481
521
482
- - **$route.name**
522
+ - **\ $ route.name**
483
523
484
524
当前路由的名称,如果有的话。(查看[命名路由](../guide/essentials/named-routes.md))
485
525
486
- - **$route.redirectedFrom**
526
+ - **\ $ route.redirectedFrom**
487
527
488
528
如果存在重定向,即为重定向来源的路由的名字。(参阅[重定向和别名](../guide/essentials/redirect-and-alias.md))
489
529
@@ -493,11 +533,11 @@ router.onError(callback)
493
533
494
534
通过在 Vue 根实例的 ` router` 配置传入 router 实例,下面这些属性成员会被注入到每个子组件。
495
535
496
- - **this.$router**
536
+ - **this.\ $ router**
497
537
498
538
router 实例。
499
539
500
- - **this.$route**
540
+ - **this.\ $ route**
501
541
502
542
当前激活的[路由信息对象](#路由对象)。这个属性是只读的,里面的属性是 immutable (不可变) 的,不过你可以 watch (监测变化) 它。
503
543
0 commit comments