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

标签 angular typescript angular6

我想获取我在 Angular 中使用 route.params 进行路由时传递的多个 id。 这是route.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。

最佳答案

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

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

  });
}

关于angular - 如何从 Angular 6 中的路由参数获取多个 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58077240/

相关文章:

node.js - 使用 Angular 和 Express 的 CORS - 在 Postman 中工作,但不在浏览器中工作

angular - 尝试在 Angular 2 应用程序中导入 npm 模块(Howler)

javascript - Angular 2 如果不在数组中则隐藏

node.js - 将后端部署到 azure 时遇到问题

javascript - 验证错误 Mongodb

angular - 如何获取仅在分页的 Material 数据表页面中显示的数据

ionic-framework - Ionic 2 Beta 和 Open Layers 3 未加载 map

typescript - 为具有多个入口点的库导出声明

angular - rxjs:EmptyError:序列中没有元素

javascript - Angular HttpInterceptor 不适用于 forkJoin