【问题标题】:How to pass Mat-Select Value to a header in API Call如何将 Mat-Select 值传递给 API 调用中的标头
【发布时间】:2020-03-13 18:47:54
【问题描述】:

我有一个 API,它需要标头中的国家代码以及授权令牌和承载。 我能够在我的组件文件中获取 mat-select 值。 但是,我的 api 标头和令牌正在服务文件中设置。 谁能帮我弄清楚如何将 mat-select 的值从组件传递到服务文件?

现在我将国家值硬编码为“au”,但我希望它根据 mat-select 下拉值进行设置。

在组件文件中获取 mat-select 值的代码:

onCountrySelection() {
console.log(this.countryValue);
sessionStorage.setItem('countryCode', this.countryValue);
}

服务类文件中的API;

uploadConfig(templateName, JsonBody) {
const header = new HttpHeaders().set(
  'Authorization',
  'Bearer ' + sessionStorage.getItem('token'),
).set(
  'country',
  'au'
);
return this.httpClient.post(
  this.localUrl + '/pattern/' + templateName + '/flow', JsonBody,
  { headers: header }
);
}

【问题讨论】:

    标签: javascript angular typescript angular8


    【解决方案1】:

    尝试在 API 方法的签名中添加额外的参数:

    uploadConfig(templateName, JsonBody, countryCode) {
    
        const header = new HttpHeaders().set(
          'Authorization', 'Bearer ' + sessionStorage.getItem('token'),
        ).set(
          'country', countryCode
        );
        return this.httpClient.post(
            this.localUrl + '/pattern/' + templateName + '/flow', JsonBody, { headers: header }
            );
    }
    

    然后当您调用服务时,只需发送countryValue

    uploadConfig(,,this.countryValue);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 2016-01-30
      相关资源
      最近更新 更多