如何从 Angular 6 中的路由参数获取多个 ID?

Shu*_*bey 6 typescript angular angular6

我想获取我在使用 route.params 在 Angular 中路由时传递的多个 id。这是 route.ts

{path:'section/:id/:id', component: SubsectionsComponent}
Run Code Online (Sandbox Code Playgroud)

这就是我从 component.ts 路由的方式

onSectionClick(id1, id2){
    this.router.navigate(['/path/',id1,id2], {relativeTo:this.route})
  }
Run Code Online (Sandbox Code Playgroud)

这就是我在它路由的组件中获取的方式。

constructor(private route: ActivateRoute){}
this.route.params.subscribe(
      (route)=>{  
        console.log(route)
      }
    )
Run Code Online (Sandbox Code Playgroud)

但它只从参数中记录一个 id。

Ste*_*pUp 5

您应该在路径中使用不同的名称。例如:

{path:'section/:id1/:id2', component: SubsectionsComponent}
Run Code Online (Sandbox Code Playgroud)

然后事情就变得非常简单:

import { ActivatedRoute } from '@angular/router';

constructor(private route: ActivatedRoute) {
  this.route.paramMap.subscribe( params => {
    this.id_1 = params.get('id1');
    this.id_2 = params.get('id2');

  });
}
Run Code Online (Sandbox Code Playgroud)