angular2和routing - 向child添加查询parm

时间:2017-03-04 19:18:04

标签: angular angular2-router3

this.router.navigate(['/services', {outlets: {'servicelistright': ['servicelist']}}]);

如果我在下面的网址,我会使用以下内容获取查询parm:

         http://localhost:4200/#/services/(servicelistright:servicelist;type=11)

this.route.params.map(params => params['type'])
            .subscribe(type => { console.log('stupid',type) });

嗯,我觉得这很棒......真棒......

但是我该如何添加查询parm?

1)使用router.navigate

this.router.navigate(['/services', {outlets: {'servicelistright': ['servicelist']}}]);

我在哪里添加type = 11 ???

或者我的routerLink

<a md-raised-button  routerLinkActive="['active']" [routerLink]="['/services']" ">Raised button</a>

1 个答案:

答案 0 :(得分:0)

参数以对象的形式传递给路径。例如,要获取此查询字符串:

/inbox/33;details=true/messages/44;mode=preview

您需要使用以下数组:

['/inbox', 33, {details: true}, 'messages', 44, {mode: 'preview'}]

因此,在您的情况下,您可以将type这样的参数传递给router.navigate

this.router.navigate(['/services', {outlets: {'servicelistright': ['servicelist', {type: 11}]}}]);
在幕后,routerLink只调用router.navigate,因此您可以将相同的URL字符串传递给指令:

<a [routerLink]="['/services', {outlets: {'servicelistright': ['servicelist', {type: 11}]}}]">Go</a>