我有一个方法:在Angular中使用Promise的正确方法?
public get(): Promise<Study[]> {
return this.http.post('', data, options)
.toPromise()
.then(response => { console.log(response) }) // response.json().result as Study[]
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('getStudyPlanView: ', error);
return Promise.reject(error.message || error);
}
是否使用无极正确的方法? 我打电话承诺喜欢为:
this.planningService.getStudyPlanView(this.globalModel.schoolId, this.globalModel.studyPeriodId)
.then(result => { })
.catch((error => console.log('ERROR loadStudyPlan():' + error)));
为什么我需要在public get()
使用.then
?和.toPromise()
有什么区别?低于或高于?
我觉得我做错了什么,因为我要求的承诺后得到一个错误:
Service.ts:82 get: SyntaxError: Unexpected end of JSON input
也许只返回'response.result'?没有json()? – OPV
'.then(response => {console.log(response)})'将解决您的承诺'undefined' – Phil
在Angular中使用Promise的“正确”方法是使用Observables。大声笑。如果你希望看到一个在Angular中使用http使用Observable的例子,请告诉我,我将在这里发布代码作为答案。 – DeborahK