我有以下代码。
getListCategoryOfQuestion() {
return this.authService.getToken()
.then(token => {
let headers = new Headers();
headers.append('Authorization', `Bearer ${token}`);
return this.http.get('http://localhost:3333/question/getCategories', {headers:headers})
.map(res => res.json())
.catch(this.handleError);
})
.catch(this.handleError);
}
我知道我无法从承诺返回同步值,但可观察的是异步的。此函数的返回类型为 Promise<Observable< > >
我只想知道如何订阅这个可观察量,目前我已经尝试过这个:
loadCategory(){
this.questionDBService.getListCategoryOfQuestion()
.subscribe(data => {
this.categories = data;
console.log(data);
});
}
但是,当然它不起作用,因为结果不是可观察的,所以有人知道如何管理这个问题吗?
一个可观察的承诺...这很好:)试试这个:
loadCategory(){
this.questionDBService.getListCategoryOfQuestion().then((observer) => {
observer.subscribe((data) => {
this.categories = data;
console.log(data);
});
});
}