【问题标题】:Angular get current active component pathAngular获取当前活动组件路径
【发布时间】:2017-08-31 03:37:56
【问题描述】:

我有以下路线,我正在尝试获取子组件的路径。

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}
        ]
      }]

为了在重新加载时获取当前路径段,我正在做这样的事情

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

即使我订阅了 url(在另一个组件中),如果子路由在 :id/create 下更改,即如果它从 022/create/general-information 更改为 022/create/another-info,我也不会收到触发器为了让子路线的每个路段都发生变化,我必须做类似

的事情

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

有没有更简单的方法来获取路径?就像一般信息或其他信息?如果是,请提供解决方案,如果不是,是什么原因。 希望我解释得很好,但如果需要更多说明,请告诉我。提前致谢。

【问题讨论】:

    标签: angular typescript angular2-routing


    【解决方案1】:

    一年后因为没有回答你可以得到它:

    this._activatedRoute.snapshot.routeConfig.path

    如果你在路上,可以说/dashboard/graphs,这会给你graphs

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2012-05-07
      • 2020-11-30
      相关资源
      最近更新 更多