如何同时使用必需参数和查询参数 - 查询参数为 null



所以我正在尝试导航到时间表路线。

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

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

我的路线.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)

我已经检查了我为开始日期和结束日期传递的值是否为非空值。

知道我在这里做错了什么吗?提前感谢...

您可以尝试使用硬编码值吗? 如果它有效,那么在将值分配给变量的位置和调用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上复制代码,以便我们可以查看它。

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

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

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

最新更新