私はデフォルトを拡張してカスタムAngular 2 httpリクエストを作成しようとしています。私は認証トークンを格納するためにIonic 2ローカルストレージを使用しています。 (将来はファイルシステムを使用する可能性が高い)。私の問題は、httpサービスから解決済みの約束を返す方法です。コンポーネント内のObservableを購読することができます。私はObservable.fromPromiseと他のバリエーションを無駄にしようとしました。返信を約束の中から見てください
request(url: string|Request, options?: RequestOptionsArgs): Observable<Response> {
// Get the token used for this request.
// * Need to return this promise resolved.
var promise = this.storage.get('token').then(token => {
if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
if (!options) {
// let's make option object
options = {headers: new Headers()};
}
options.headers.set('Authorization', 'Basic ' + token);
} else {
// we have to add the token to the url object
url.headers.set('Authorization', 'Basic ' + token);
}
return super.request(url, options).catch(this.catchAuthError(this));
}).catch(error => {
console.log(error);
});
}
アイデアはこのブログの投稿に基づいていますが、イオンストレージは約束を返します。 http://www.adonespitogo.com/articles/angular-2-extending-http-provider/
「user」は誰ですか? –
トークンが必要です。 –
「then」の中で観測可能なものを扱うのは便利ではありません。私はそれが 'Observable.fromPromise(this.storage.get( 'token')))のようなものでなければならないと思います。 ..)) ' – estus