In beta version from the child component (let's say 'child1'
) you could tell your parent to load (navigate to) a different child component instead of current by something like that:
_router.parent.navigate(['child2']);
('_router' is injected in constructor)
In RC1 there's no 'parent' property anymore so it seems like the you have to use _router.navigate()
and provide the full url starting from the app root. Something like
/root/grandparent/parent/child2
Now, the question is if the child component is only aware about parent's routes but not about grandparent - how do you do this? Would be nice to use _router.navigate(['../child2'])
but that gives me a weird errors like "invalid number of '../'.
The only way to get parent's url I found so far is that:
_router.routeTree._root.children[0].value.stringifiedUrlSegments
(you can also get there from OnActivate event)
but this looks just wrong to me. It also might differ depending on the number of parents/grandparents - I haven't tried yet.
So, is there a better way to do that or relative path should work and I'm doing something wrong?
Thank you.
I had the same problem. Just today I have found solution (it was in router.ts file here line:89)
/**
* Navigate based on the provided array of commands and a starting point.
* If no segment is provided, the navigation is absolute.
*
* ### Usage
*
* ```
* router.navigate(['team', 33, 'team', '11], segment);
* ```
*/
navigate(commands: any[], segment?: RouteSegment): Promise<void> {
return this._navigate(this.createUrlTree(commands, segment));
}
You need to import RouteSegment and add it you your constructor
import { Router, RouteSegment } from '@angular/router';
...
contructor(private $_router:Router, private $_segment:RouteSegment){}
...
//If you are in for example child1 and child 1 is sibling of child2
this.$_router.navigate(['../child2'],this.$_segment);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.