angular - Angular2-router: How to only change a parameter of a route? -
i want have following routes in application:
export const routes: routes = [ { path: ':universityid', component: universitycomponent, children: [ { path: 'info', component: universityinfocomponent }, { path: 'courses/:status', component: coursesbystatuscomponent } ] } ]
being on /1/info
or /1/courses/open
url, how change :universityid
universitycomponent
?
simple router.navigate(['../', 2], {relativeto: currentroute })
won't because redirects /2
, losing other information. using '../2/courses/open
not option - can on child route @ moment. best come is:
const urltree = this.router.parseurl(this.router.url); urltree.root.children['primary'].segments[0].path = '2'; this.router.navigatebyurl(urltree);
but it's kind of ugly
here code go along comment above using tuples: replicate pattern twice... shows how integrate these classes project. so:
- change data-component-resolver.ts parent-child-resolver.ts code in link.
- replace data-component.ts in link parent-child.component.ts below
parent-child-resolver.ts
import {injectable} '@angular/core'; import {observable} 'rxjs'; import {resolve, activatedroutesnapshot, routerstatesnapshot} '@angular/router'; import {parent} '../shared/model/parent'; import {children} '../shared/model/children'; import {parentservice} '../providers/parent.service'; @injectable() export class parentchildresolver implements resolve<[parent,(children[])> constructor(private parentservice : parentservice) {} resolve( route: activatedroutesnapshot, state: routerstatesnapshot ) : observable<[parent,(children[])> { return this.parentservice.findparentbyid(route.params['id']) .switchmap(parent => this.parentservice.findchildrenforparent(parent.id), (parent, children) => [parent, children] ); } }
parent-child.component.ts
import {component, oninit} '@angular/core'; import {parent} '../shared/model/parent'; import {children} '../shared/model/children'; import {observable} 'rxjs'; import {activatedroute} '@angular/router'; @component({ selector: 'parent-child', templateurl: './parent-child.component.html' }) export class parentchildcomponent implements oninit{ $parent: observable<parent>; $children: observable<children[]>; constructor(private route:activatedroute) {} ngoninit() { this.parent$ = this.route.data.map(data => data['key'][0]); //from router.config.ts this.children$ = this.route.data.map(data => data['key'][1]); } }
Comments
Post a Comment