【问题标题】:How to get multiple ids from route param in Angular 6?如何从Angular 6中的路由参数获取多个ID?
【发布时间】:2020-01-24 09:32:12
【问题描述】:

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

{path:'section/:id/:id', component: SubsectionsComponent}

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

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

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

constructor(private route: ActivateRoute){}
this.route.params.subscribe(
      (route)=>{  
        console.log(route)
      }
    )

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

【问题讨论】:

  • 在路径中使用不同的名称。 :id1/:id2 例如

标签: angular typescript angular6


【解决方案1】:

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

{path:'section/:id1/:id2', component: SubsectionsComponent}

然后就变得很简单了:

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');

  });
}

【讨论】:

    【解决方案2】:

    路由中的参数不能有重复的名称,它会覆盖值。

    使用类似这样的其他名称。

    {path:'section/:id/:id1', component: SubsectionsComponent}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多