我有一个方法,该方法首先通过订阅获取令牌的方法来获取令牌,然后从http请求中返回Observable。问题是它仅在调用subscribe时返回,但是返回undefined或之前我返回的末尾,我如何通过订阅方法返回Observable呢?
public addArticle(data): Observable<Article> {
this.getToken().subscribe(() => {
const httpOptions = {
headers: new HttpHeaders({
'Authorization': ('Bearer ' + this.accessToken),
'Accept': 'application/json',
'content-type': 'application/json'
}),
}
var file = (document.querySelector('input[type=file]')).files[0];
var reader = new FileReader();
let icon: string;
var _this = this;
reader.addEventListener("load", function () {
icon = reader.result.toString();
icon = icon.substring(icon.indexOf(',') + 1);
return _this.http.post(url,body/*(including icon)*/, httpOptions);
}, false);
if (file) {
reader.readAsDataURL(file);
} else {
return this.http.post(url,body/*without icon*/, httpOptions);
}
});
}