嵌套路由链接 Angular 4

Nested Routing Links Angular 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)

所以,现在如果我在 service1.componenet 中使用 router.navigateByUrl,我将不得不这样做像这样:

this.router.navigateByUrl('/client/service2');

我将不得不继续嵌套路由模块,所以稍后知道路由的 "parents" 可能是个问题,我想知道是否有更 有效的解决方案 而不是复制整个路线,例如:

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
    });
  }
}