我熟悉Angular2,Ionic2和 ,可能我误解了某些内容,但希望得到帮助。在Angular 2中返回承诺值,Ionic 2
我有一个名为'CurrentUser'的提供者用于存储和获取LocalStorage数据。
getProfile(): any {
this.local.get("user-profile").then((profile) => {
var val = JSON.parse(profile);
return val;
});
}
此功能。如果我注入此提供到一个组件getProfile()
返回一个承诺
。在从组件调用此函数时,如何在分配数据之前等待解决的承诺?
@Component({
templateUrl: 'build/pages/book_meeting/book_meeting.html'
})
export class BookMeetingPage implements OnInit {
constructor(public navCtrl: NavController, private _currentUser: CurrentUser) {
}
profile: IProfile;
ngOnInit(): any {
this.profile = this._currentUser.getProfile();
console.log(this.profile);//returns undefined
}
}
您好,我得到的错误app.bundle.js: 44529 EXCEPTION:错误:未捕获(承诺):TypeError:无法读取未定义的属性'then'。 – Arianule
@Arianule抱歉,我在回答中提到,但忘记在代码部分更改同样的东西,我的坏。感谢您的支持。检查更新的代码 –
嗨那里Pankaj。你有没有更新代码,对不起。我应该返回一个Promise ...沿Promise.resolve(val)返回的行吗? – Arianule