如何使用一个响应在另一个没有ID值的方法打字稿中

时间:2018-01-25 05:41:15

标签: angular typescript angular5

来自服务响应,需要在另一个组件的ngOnit函数中获取,但在该打字稿中没有 ids

此处 ID 表示

detailvisit(visit: Visit) {
     const ids = {
      'ID': visit.ID,
      'patientid': visit.pid,
    };
    this.visitService.getavisit(ids);
    this.router.navigate(['/detailvisit']);
  }

获取记录的服务

getavisit(ids): Observable<Visit> {
    const headers = new Headers({ 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + this.authenticationService.token });
    const options = new RequestOptions({headers: headers });
  return this.http.post('/api/getvisit/', JSON.stringify(ids), options)
  .map((response: Response) => {
                   return <Visit>response.json();
            });

1 个答案:

答案 0 :(得分:1)

您需要使用.subscribe并将router.navigate放在

this.visitService.getavisit(ids).subscribe( response => {
                     console.log("Success Response" + token)
                     this.router.navigate(['/detailvisit']);
                   },
 error =>  console.log(error));