Я столкнулся с проблемой, когда инициировал значение в классе моей страницы constructor
. Я вызываю provider
для загрузки данных. Внутри вызова service
я вижу, что данные были вызваны, но когда я вызвал свои переменные вне вызова службы, он сказал undefined
. Что случилось?
HomePage.ts
export class HomePage {
public data: any;
constructor(private nav: NavController, private auth: Auth, private params: NavParams) {
this.auth.getProfile().then((profile) => {
this.data = profile;
console.log('here i can see data correctly ' + JSON.stringify(this.data));
})
console.log('here data undefined ' + JSON.stringify(this.data));
}
поставщик auth.ts
getProfile(){
return this.getToken().then((token) => {
// console.log(token);
if (token) {
return new Promise(resolve => {
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
try {
this.http.get('http://laraveldev/api/users/get_user?token=' + token).subscribe(data => {
this.profile = data.json().data;
//console.log('getProfile ' + JSON.stringify(this.profile));
resolve(this.profile);
});
} catch (error) {
console.log(error);
this.data = { status: 'error' };
resolve(this.data);
}
});
}
});
}