嵌套路由链接角4
问题描述:
这是我当前设置:嵌套路由链接角4
- app (Folder)
- app-routing.module.ts (File)
- client (Folder)
- client-routing.module.ts (File)
- service (Folder)
- service-routing.module.ts (File)
- service1.componenent.ts (File)
- service2.componenent.ts (File)
所以,现在如果我使用router.navigateByUrl内service1.componenet我必须做这样的:
this.router.navigateByUrl('/client/service2');
我必须保持嵌套路由模块,因此知道“父母”的扣肉 TE以后可能是一个问题,我想知道是否有一个更有效的解决方案而不是复制整条路线,是这样的:
this.router.navigateByUrl(parentRoute + '/service2');
凡parentRoute它的嵌套路线的汇编。
答
您可以尝试在service1component
中使用ActivatedRoute
。 类似这样的:
import { Router, ActivatedRoute } from '@angular/router';
@Component({
...
})
export class Service1Component {
constructor(
private _router:Router,
private _route: ActivatedRoute
){}
...
navigate(){
this._router.navigate(['./service2'],{
relativeTo: this._route
});
}
}