angular - 使用延迟加载以 Angular 获取路由参数

标签 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作为值(value)。

当我在这条路径上时它正在工作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"
      }
    ]
  }
];

最佳答案

您没有在 LastCC 上配置参数路线。尝试这样的事情:

  {
    path: ":id",
    component: DashboardComponent
  },
  {
     path: ":id/LastCC",
     loadChildren:
          "app/useful-documents/credit-point/credit-point.module#CreditPointModule"
   }

请注意,LastCC 路由的路径现在有一个路由参数:path: ":id/LastCC"
另请注意,LastCC 路由不再是子路由。有必要吗?

如果它确实需要是子路由,那么您可能需要保留路由配置并从父路由而不是 LastCC 中读取参数。路线。
this.activatedRoute.parent.paramMap.subscribe(params => {
  console.log("params******");
  console.log(params.get("id"));
});

注意 .parentactivatedRoute 之后.

关于angular - 使用延迟加载以 Angular 获取路由参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655627/

相关文章:

javascript - 如何仅当组件 Angular 4 中的服务变量状态更改时才运行函数

javascript - 无法使用在 Angular 7 中创建的自定义管道进行搜索

Angular 5 : Conditionally Set Default Route

angular - 如何对 http 401 做出 Angular 响应

angular - 当用户使用 f5 重新加载页面时,如何以 Angular 正确进行?

php - 将 Angular 4 与 LARAVEL 5.4 连接起来

node.js - 带有 TypeScript : Declaration expected compiler error after @component 的 Angular2

javascript - Angular Promise.all 在超时事件中不起作用

angular - 读取路由参数?

javascript - Angular : reload page after redirection