如何将userId传递到Angular中的标头

时间:2018-06-21 20:03:39

标签: angular typescript http http-headers

我正在尝试将userId传递给此调用中的标头:

  

api.service.ts

onUpdateJamItMessageBoard( dispMsg: any, dispStartTs: any, dispEndTs: any, userId: any) {
    const url = this.updateJamItMessageBoard + 'messages';
    const params = new HttpParams()
      .set('dispMsg', String(dispMsg))
      .set('dispStartTs', String(dispStartTs))
      .set('dispEndTs', String(dispEndTs));
      const headers = new HttpHeaders({
        'userId': String (userId)
      });


return this.http.post<JamIt>(url, null, {
  params: params, headers: headers
  });
}

我在这里订阅:

  

jamit.component.ts

onUpdateJamIt() {
    let dispMsg = this.jamItMessageForMessageCenter + ' ' + this.jamItMessage + ' ' + this.freeText;
    let dispStartTs = this.startTimer;
    let dispEndTs = this.endTimer;
    let userId = '';

    this.apiEndPointService.onUpdateJamItMessageBoard(dispMsg.trim(), dispStartTs.trim(), dispEndTs.trim(), userId.trim()).subscribe(response => {
      console.log(response)
    });

  }

,这是我在HTML中使用它的地方:

  

jamit.component.html

<button mat-raised-button color="warn" (click)="onUpdateJamIt()">Update</button>


当我单击click事件时,我传递了除UserId之外的所有其他参数,我不知道为什么。请帮忙。我做错了吗?还是我想念的东西?

1 个答案:

答案 0 :(得分:1)

尝试一下-

const headers = new HttpHeaders();
headers.set('userId', userId);

有关更多信息,请在此处查看-