angular2 and routing - adding a query parm to child

浪尽此生 提交于 2020-01-05 05:39:06

问题


Wow

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

If I the below to the url I get get the query parm using the below:

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

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

Well I think is just wonderful..wow great...

But how on earth do I add the query parm?

1) by using router.navigate

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

where do I add type=11???

Or my routerLink

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

回答1:


Parameters are passed to the route in a form of an object. For example, to get this query string:

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

you need to use the following array:

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

So in your case you can pass the type param like that to router.navigate:

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

behind the scenes, routerLink just calls router.navigate, so you can pass the same URL string to the directive:

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


来源:https://stackoverflow.com/questions/42600241/angular2-and-routing-adding-a-query-parm-to-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!