【发布时间】:2019-11-06 01:12:25
【问题描述】:
我正在尝试提交帖子,并发送带有标头的令牌。但是我的 storage.get 返回一个承诺,我不知道如何从 storage.get 中获取令牌的值。我认为将其转换为 observable 可能会有所帮助,但我不知道该怎么做。
sendPostRequest() {
var token: string;
this.storage.get('ACCESS_TOKEN').then((val) => {
token = val;
});
const headers = new HttpHeaders()
.set('Accept', 'application/json')
.set('Content-Type', 'application/json')
.set('Authorization', 'Bearer ' + token)
.set('responseType', 'text');
let postData = this.signatureForm.value;
this.httpClient.post("http://localhost:3000/signature", postData, { headers: headers })
.subscribe(data => {
this.presentToast();
}, error => {
this.showError = true;
this.errorMessage = error.error.message
});
}
【问题讨论】:
-
如果你真的想把你的promise转换成一个observable,你可以使用RxJS的
from()函数。但正如答案中提到的那样,你真的不需要它。