Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何在angular 7中的激活链接中获取url参数并从服务类调用rest api_Json_Angular_Typescript_Angular Ui Router_Angular7 - Fatal编程技术网

Json 如何在angular 7中的激活链接中获取url参数并从服务类调用rest api

Json 如何在angular 7中的激活链接中获取url参数并从服务类调用rest api,json,angular,typescript,angular-ui-router,angular7,Json,Angular,Typescript,Angular Ui Router,Angular7,我想使用id字段和Get参数获取数据。 下面是我的html url代码,它重定向到特定页面,但不调用服务获取RESTAPI <a [routerLink]="['/usedCars/detail', lists.id]" class="btn btn-danger">Read more...</a> 下面是一个邮递员回复的示例 { "car_id": "0", "car_name": "Nissan", "car_model": "Sunny

我想使用id字段和Get参数获取数据。 下面是我的html url代码,它重定向到特定页面,但不调用服务获取RESTAPI

  <a [routerLink]="['/usedCars/detail', lists.id]" class="btn btn-danger">Read more...</a>
下面是一个邮递员回复的示例

{
    "car_id": "0",
    "car_name": "Nissan",
    "car_model": "Sunny",
    "car_built": "Nissan inc",
    "car_manufac_year": "2019",
    "usedcarimages": [
        "0_Nissan-1.jpg",
        "0_Nissan-2.jpg"
    ]
}

我将此网站用作参考

我认为您的代码中唯一的潜在问题是使用
swtichMap()

当前-

switchMap((params: ParamMap) =>
  this.autopostService.getcarDetail(params.get('id'))
)
switchMap((params: ParamMap) => {
  return this.autopostService.getcarDetail(params.get('id'))
})
如果使用的是
swtichMap()
,则代码指令
this.autopostService.getcarDetail(params.get('id'))
应该与
swtichMap()的指令在同一行中,如果使用的是箭头函数,则需要提及显式
return
语句

正确的方法-

switchMap((params: ParamMap) =>
  this.autopostService.getcarDetail(params.get('id'))
)
switchMap((params: ParamMap) => {
  return this.autopostService.getcarDetail(params.get('id'))
})


您收到的错误是什么?没有,我没有收到任何错误,但它也没有从服务类调用api请求。请检查下面的解决方案。@nilay您需要在
this.autopostService….
之前指定
return
。正确检查这两种方法。是的,添加了返回,但仍然没有调用服务方法。@nilay您可以用您在编辑器中更改的代码更新上面的问题。@nilay您可以在这里进行stackBlitz吗
switchMap((params: ParamMap) => {
  return this.autopostService.getcarDetail(params.get('id'))
})
switchMap((params: ParamMap) => this.autopostService.getcarDetail(params.get('id')))