Javascript 如何在API调用中将Mat Select值传递给头
我有一个API,它需要在标题中包含国家代码以及授权令牌和承载。 我能够在我的组件文件中获取mat select值。 但是,我的api头和令牌正在服务文件中设置。 有人能帮我弄清楚如何将mat select的值从组件传递到服务文件吗 现在,我硬编码的国家值为'au',但我希望它设置为根据mat选择下拉值 在组件文件中获取mat select值的代码:Javascript 如何在API调用中将Mat Select值传递给头,javascript,angular,typescript,angular8,Javascript,Angular,Typescript,Angular8,我有一个API,它需要在标题中包含国家代码以及授权令牌和承载。 我能够在我的组件文件中获取mat select值。 但是,我的api头和令牌正在服务文件中设置。 有人能帮我弄清楚如何将mat select的值从组件传递到服务文件吗 现在,我硬编码的国家值为'au',但我希望它设置为根据mat选择下拉值 在组件文件中获取mat select值的代码: onCountrySelection() { console.log(this.countryValue); sessionStorage.setI
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 }
);
}
尝试向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);