承诺并订阅 [英] Promise and subscribe
问题描述
我有一个Angular2(ionic2)应用程序.我有一个请求城市的函数,但我收到一个错误,指出this.cityService.getAllCities()
上不存在属性订阅.
I have an Angular2 (ionic2) application. I have a function that request cities but I get an error that Property subscribe does not exists on this.cityService.getAllCities()
.
cityPage.ts具有如下功能:
cityPage.ts has a function like this:
getCities(){
this.cityService.getAllCities()
.subscribe(cityData => { this.cityList = cityData; },
err => console.log(err),
() => console.log('Complete!')
);
}
我的cityService.getAllCities()函数如下所示:
my cityService.getAllCities() function looks like this:
getAllCities(){
return new Promise (resolve => {
this.storage.ready().then(() => {
this.storage.get('authData').then(authData => {
let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' +
authData.access_token });
let opt = new RequestOptions({ headers: hdr });
return this.http.get(AppSettings.API_GET_CITIES).map(res => <CityModel[]> res.json(), opt);
}).catch(() => {
//resolve(false);
});
});
});
}
修改
根据评论,我已更改了如下功能:
Based on the comment I've changed my function like this:
getAllCities(){
return Observable.create(resolve => {
this.storage.ready().then(() => {
this.storage.get('authData').then(authData => {
let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' +
authData.access_token });
console.log('access_token ' + authData.access_token);
let opt = new RequestOptions({ headers: hdr });
return this.http.get(AppSettings.API_GET_CITIES,opt).map(res => <CityModel[]> res.json()).subscribe((result) => {
console.log(result);
resolve = result;
});
}).catch(() => {
//resolve(false);
});
});
});
}
在我的console.log(result)
中,我接收到数据,但是数据从未返回到我的getCities()
函数中.同样,console.log('Complete!')
也不会被调用.
In my console.log(result)
I receive data, but the data is never returned to my getCities()
function. Also the console.log('Complete!')
is not called.
推荐答案
之所以引发错误,是因为.subscribe
方法确实在Observable
上可用,只要它发出数据就可以监听.在这里,从getAllCities
方法返回的是promise
,您可以在其上应用.then
函数以获取从该Promise
The reason it is throwing an error, because .subscribe
method does available on Observable
to listen to, whenever it emits a data. Here from getAllCities
method you're returning a promise
you could apply .then
function over it to get data returned from that Promise
getCities() {
this.cityService.getAllCities()
.then(
cityData => { this.cityList = cityData; },
err => console.log(err),
() => console.log('Complete!')
);
}
还可以通过在http.get()
Observable上调用.toPromise()
方法来从getAllCities
方法返回promise.
And also return promise from getAllCities
method by calling .toPromise()
method over http.get()
Observable.
getAllCities(){
return new Promise (resolve => {
this.storage.ready().then(() => {
this.storage.get('authData').then(authData => {
let hdr = new Headers({'Content-Type': 'application/json', 'Authorization': 'Bearer ' +
authData.access_token });
let opt = new RequestOptions({ headers: hdr });
//returned promise from here.
return this.http.get(AppSettings.API_GET_CITIES)
.map(res => <CityModel[]> res.json(), opt)
.toPromise();
}).catch(() => {
//resolve(false);
});
});
});
}
这篇关于承诺并订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!