如何同时使用Required和Query参数-查询参数为null

时间:2019-09-03 14:13:46

标签: angular angular-router angular-route-guards

所以我试图导航到时间表路线。

路由以员工的姓名作为路由器参数,以开始日期和结束日期作为查询参数。

this.router.navigate(['/timesheet', this.name], { queryParams: { start_date: startString, end_date: endString }});

我的route.js看起来像这样:

{
  path: 'timesheet/:artistName',
  component: TimesheetComponent,
  canActivate: [RoleGuardService],
  data: {roles: ['consume:admin', 'consume:exec', 'consume:producer' ]},
  resolve: {
    timesheet: TimesheetResolve
  },
}

当我尝试点击REST后端时形成的URL在start_date和end_date中具有空值。

GET http://localhost:4200/api/v1/timesheet/artist/Jimmy?start_date=null&end_date=null 500 (Internal Server Error)

我检查了我要传递的开始日期和结束日期的值都不为空。

有什么主意我在这里做错了吗?预先感谢...

2 个答案:

答案 0 :(得分:1)

您可以尝试使用硬编码的值吗?如果可行,则在将值分配给变量和调用this.router之间存在问题。

this.router.navigate(['/timesheet', this.name], { queryParams: { start_date:'12-Aug-2019', end_date:'12-Sep-2019' }});

检查导航栏如何显示http请求,它应为:

http://localhost:4200/api/v1/timesheet/artist/Jimmy?start_date=12-Aug-2019&end_date=12-Sep-2019

如果这也不起作用,则可以在stackblitz上复制代码,以便我们对其进行研究。

这应该不是路由器的问题,因为其余网址的格式正确。

答案 1 :(得分:0)

这是由于我使用的解析器尝试使用

获取查询参数引起的
return this.artistService.getTimesheet(route.paramMap.get('artistName'),
  route.paramMap.get('start_date'), route.paramMap.get('end_date'));

而不是尝试这样做:

return this.artistService.getTimesheet(route.paramMap.get('artistName'),
  route.queryParamMap.get('start_date'), route.queryParamMap.get('end_date'));