我需要使用@Ionic/storage
从存储中获取承载令牌。
Ionic存储器返回一个承诺。
然后我需要在angular http
请求中使用它,它将返回Observable。
我怎样才能达到同样的目标?或者我可以使用@Ionic/Storage
作为Observable获得纯返回类型吗?
get(url: string, options?: RequestOptionsArgs): Observable<any> {
let promise = this.requestOptions(options).then((options) => {
return super.get(this.getFullUrl(url), options)
.catch(this.onCatch)
.do((res: Response) => {
this.onSubscribeSuccess(res);
}, (error: any) => {
this.onSubscribeError(error);
})
.finally(() => {
this.onFinally();
});
});
return Observable.fromPromise(promise);
}
RequestOptions函数:
private requestOptions(options?: RequestOptionsArgs): Promise<RequestOptionsArgs> {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
return this.currentUser.currentToken().then((token) => {
options.headers = new Headers({ 'Content-Type': 'application/json' });
options.headers.append('Authorization', 'Bearer ' + token);
return this.currentUser.getAppVersion().then((version: string) => {
options.headers.append("appVersion", version);
return options;
})
})
} else {
return null;
}
}