如何在Angular 2中以Promise类型返回数据?
使用此函数我需要以承诺类型返回数据:
private getStudyPeriods(): Promise<CurrentPeriod> {
let data = [];
return data;// here
}
我尝试了这样的最后一个变体:
public getStudyPeriods(): Promise<Period[]> {
return this.education.get(7).then((data: any) => {
if (!data.hasOwnProperty('errors')) {
this.periods = data;
return new Promise((resolve, reject) => {
resolve(this.periods);
});
}
})
}
答案 0 :(得分:6)
立即返回Promise
结果本身。
private getStudyPeriods(): Promise<CurrentPeriod> {
let data = [];
return new Promise(resolve => {
resolve(data);
});
}
答案 1 :(得分:2)
private getStudyPeriods(): Promise<CurrentPeriod[]> {
return new Promise((resolve, reject) => {
let data: CurrentPeriod[] = [];
resolve(data);
});
}
我将返回类型更改为数组,因为您似乎正在尝试返回CurrentPeriod数组,而不仅仅是一个。