延迟加载获取角度的路径参数

时间:2018-08-02 14:04:57

标签: angular angular-router

我将从Angular 5的路径中检索参数

http://localhost:4200/#/dashboard/74618/LastCC

我将从74618参数的路由中获得id

我已经这样处理了

constructor(public activatedRoute: ActivatedRoute)


this.activatedRoute.paramMap.subscribe(params => {
  console.log("params******");
  params.get("id");
});

我得到undefined作为值。

当我在此路径http://localhost:4200/#/dashboard/74618/上工作时(没有\LastCC

LastCC是延迟加载的,这是路由器配置:

const routes: Routes = [
  {
    path: "",
    component: CreditPointComponent
  }
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class CreditPointRoutingModule {}

id参数位于父模块路由器配置(仪表板模块)上。

const routes: Routes = [
  {
    path: ":id",
    component: DashboardComponent,
    children: [
      {
        path: "LastCC",
        loadChildren:
          "app/useful-documents/credit-point/credit-point.module#CreditPointModule"
      }
    ]
  }
];

1 个答案:

答案 0 :(得分:1)

您没有在└───app ├───config ├───controllers └───models 路由上配置参数。尝试这样的事情:

LastCC

请注意,LastCC路由的路径现在具有一个路由参数: { path: ":id", component: DashboardComponent }, { path: ":id/LastCC", loadChildren: "app/useful-documents/credit-point/credit-point.module#CreditPointModule" }

还请注意,LastCC路由不再是子路由。需要吗?

如果确实是子路由,那么您可能需要保留路由配置,并从父路由而不是从path: ":id/LastCC"读取参数路线。

LastCC

this.activatedRoute.parent.paramMap.subscribe(params => { console.log("params******"); console.log(params.get("id")); }); 之后注意.parent

相关问题