typescript - Angular mevcut aktif bileşen yolunu al


Aşağıdaki rotalara sahibim ve çocuk bileşenin yolunu almaya çalışıyorum.

const myRoutes: Routes = [
  {
    path: '',
    component: mainComponent
  }, {
    path: ':id/create',
    component: anothercomponent,
    children: [
      { path: '', redirectTo : 'general-information', pathMatch: 'full' },
      { path: 'general-information', component: GeneralInfoComponent},
      { path: 'another-info', component: AnotherInfoComponent}
        ]
      }]

Yeniden yüklemede mevcut yol segmentini almak için bu şekilde bir şey yapıyorum

route.firstChild.url.subscribe(val => {
      let myRoute = val[0].path;
    });

Başka bir komponentte (anotherComponent) url’e abone olmama rağmen, çocuk rota :id/create altında değişirse tetiklemeleri almıyorum, yani 022/create/general-information’den 022/create/another-info’ya değişirse ve çocuk rotalar için her segment değişikliğini almak için şöyle bir şey yapmam gerekiyor

router.events.subscribe(event:Event => {
    if(event instanceof NavigationEnd) {
    this.route.firstChild.url.subscribe(val => {
    let myroute = val[0].path;
    });
    }
    })

Yalnızca yolu almak için daha kolay bir yol var mı? Mesela sadece general-information veya another-info gibi? Eğer varsa çözümü paylaşır mısınız, yoksa sebep nedir? Umarım bunu güzelce açıkladım, ama daha fazla açıklama gerekirse bana bildirin. Şimdiden teşekkürler.

Cevap #1

kimse cevap vermediği için bir yıl sonra şöyle alabilirsiniz:

this._activatedRoute.snapshot.routeConfig.path

eğer rota üzerindeyseniz diyelim ki /dashboard/graphs bu size graphs verecektir