[英]observable and promises in angular2 and ionic2 request fails
在承諾請求解決后嘗試執行可觀察的操作,但無法找到如何
經過一番研究,我發現ionic2存儲返回了一個承諾,我想執行一個http請求,該請求應該返回一個可觀察到的
這是我嘗試過但失敗的
constructor(storage:Storage){}
getAll():Observable<any>{
this.storage.get("token")
.then(res=>{ //perform the get request after value of token is found
if(res){
return this._http.get(this.companiesurl+"?access-token="+res)//attach token
.map(res=>res.json().data)
}
}
}
上面的函數在構造函數中調用
this.chojeService.getAll().subscribe(respose => {
this.users = response
},
如果您有一個Promise
,則可以很容易地將其包裝在Observable
:
const data: Observable<any> = Rx.Observable.fromPromise(this.storage.get("token"))
.flatMap(res => res ? this._http.get("...") : Rx.Observable.of(undefined))
.map(res => res.json().data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.