【问题标题】:Get two parameters from URL从 URL 获取两个参数
【发布时间】:2020-06-29 09:22:17
【问题描述】:

我有一个表单,它采用一个参数来构造客户并将其链接到产品。现在我们添加了另一个参数以包含在 URL 中,但是我似乎无法从 URL 中获取它。

我做错了什么?感谢您的帮助!

网址:http://localhost:4200/check/98712?Id=803

我可以设法从“98712”中获取第一个参数,但不是 Id。

在我的app.module下

RouterModule.forRoot([
{ path: 'check/:cust', component: CheckComponent},

我曾尝试在此处添加 ID,但它不起作用。

在我的 Check 组件中,我取出了客户和 Id,但 Id 仍然是空的。

this.customer = this.route.snapshot.paramMap.get('cust'); 
this.Ids = this.route.snapshot.paramMap.get('Id'); //Ids since it could be several, hence the add of Id in the URL

我在 app-routing 中的路线

const routes: Routes = [
  { path: '', redirectTo: 'ms', pathMatch: 'full' },
  { path: 'ms', component: CheckComponent },
];


@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

如果您需要更多代码,请告诉我。

【问题讨论】:

标签: angular typescript


【解决方案1】:

你的id是一个查询参数,所以你需要从queryParamMap获取。

this.customer = this.route.snapshot.paramMap.get('cust'); 
this.Ids = this.route.snapshot.queryParamMap.get('Id');

【讨论】:

  • 你是英雄!我在这里被蒙蔽了,完全错过了。我会尽快标记为答案。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-02-11
  • 2017-12-18
  • 2014-05-12
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多