如何从Angular 6中的路线参数获取多个ID? [英] How to get multiple ids from route param in Angular 6?

查看:90
本文介绍了如何从Angular 6中的路线参数获取多个ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取在使用route.params在Angular中进行路由时传递的多重ID.这是route.ts

I want to fetch multiples ids which I am passing while routing in Angular using route.params. This is the route.ts

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

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

And this is how I am routing from a component.ts

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

这就是我要提取其路由组件的方式.

And this is how I am fetching in the component where it routes.

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

但这只是从参数中记录一个id.

But it is only logging one id from the params.

推荐答案

您应在路径中使用其他名称.例如:

You should use different names in your path. For example:

{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 6中的路线参数获取多个ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆