Javascript 角度路由器能否公开父级–;当前–;下一条路线?
我有一个模块,最初重定向到Javascript 角度路由器能否公开父级–;当前–;下一条路线?,javascript,angular,angular-routing,Javascript,Angular,Angular Routing,我有一个模块,最初重定向到 redirectTo: "/profile/bbb" 其中,profile有两个子项: { path: '', component: ProfileComponent, children: [{ path: 'aaa', component: AComponent }, { path: 'bbb', component: BComponent }] } 现在-在prof
redirectTo: "/profile/bbb"
其中,profile
有两个子项:
{
path: '',
component: ProfileComponent,
children: [{
path: 'aaa',
component: AComponent
}, {
path: 'bbb',
component: BComponent
}]
}
现在-在profile的构造函数中,我想知道当前路由(即profile
)是什么,以及它将要执行的下一个路由(即bbb
)
代码如下:
profile.component.ts
constructor(private route: ActivatedRoute) {
route.children[0].url.subscribe(f => console.log(f))
}
但它告诉我:
我想我只是询问了路线的结构,而不是当前的活动路线
问题
在路线上的每条路径中,我如何知道父路线、当前路线以及它将导航到哪里
ps我不想通过快照解决方案来实现,因为组件可以重用
您可以使用ActivatedRoute执行此操作 像这样导入它:
import { ActivatedRoute } from '@angular/router';
现在将其注入组件:
constructor( private route: ActivatedRoute) {
console.log('Parent URL:', route.parent.url['value'][0].path);
console.log('Child URL:', route.firstChild.url['value'][0].path);
}
如果您想知道每个导航的完整URL。
然后,您可以使用路由器来实现这一点。可能在AppComponent中
import { Router, NavigationStart, NavigationEnd } from '@angular/router';
组件的外观如下所示:
constructor(private router: Router) {
router.events.subscribe((route) => {
if (route instanceof NavigationStart) {
console.log("NavigationStart called:", route.url);
}
if (route instanceof NavigationEnd) {
console.log("NavigationEnd called:", route.url);
}
});
}
您将使用来自路由器模块的ActivatedRoute类获得当前路由,该类将为您提供所需的一切您所说的“下一个路由”是什么意思?next作为子数组中的next,或者作为用户导航到的任何其他路由?@TomaszKula它不必是子路由。顺便说一句,我们可以将路由声明为兄弟路由,例如
beatles/john
和beatles/paul
,因此paul和john不必声明为子路由。我想要的实际上是在`/profile/bbb/c/v/b
,如果我在c
的构造函数中,那么父级是bbb
,下一个是v
。我想知道的是我目前在路线的哪一部分,谁是家长,我要去的下一个部分是什么。我这里不是说用户操作。我想你最好的办法是解析router.config。但它不适用于惰性加载(loadChildren)。更新了答案。顺便说一句,您显示了父级和子级。现在呢?那么,如果看a/b/c,我们在b的ctor中,那么我们能得到b吗?