在子组件的beta版本中(假设为'child1'),您可以告诉您的父级加载(导航到)其他子组件,而不是通过类似的方式加载当前子组件:
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)在RC1中,不再有'parent'属性,因此您似乎必须使用_router.navigate()并从应用程序根目录开始提供完整的url.
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现在,问题是子组件是否仅了解父级的路由而不是祖父母级的路由-您如何做到这一点? 使用_router.navigate(['../child2'])会很好,但这给了我一个奇怪的错误,例如"invalid number of '../'.
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(您也可以通过OnActivate事件到达那里)
(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?
谢谢.
推荐答案我遇到了同样的问题.就在今天,我找到了解决方案(它位于router.ts文件此处行:89)
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)); }您需要导入RouteSegment并将其添加到您的构造函数中
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);更多推荐
Angular 2新(RC1)路由器.嵌套路线
发布评论