我有一个简单的系统,带有来自Firebase的用户身份验证系统。我想要接收当前登录用户的数据列表。通过订阅Firebase中的observable,我可以从当前登录的用户那里获取信息。在Angular2中执行函数之前获取可观察值
但是,在构造函数中设置的private authId
的值在调用getAllEmails()
函数之前未调用。因此${this.authId}
产量未定义。在我的getAllEmails
函数被调用之前,我如何确保从observable中获得authId的准备好值?
import { Injectable } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import { Email } from '../email';
import { AuthService } from '../auth/auth.service';
@Injectable()
export class EmailService {
private authId: string;
constructor(
private af: AngularFire,
private authService: AuthService
) {
this.authService.getUserInformation()
.subscribe(user => {
this.authId = user.authId;
})
}
getAllEmails(): FirebaseListObservable<any> {
return this.af.database.list(`/emails/${this.authId}`);
}
}
你在哪里调用getAllEmails()? ngOnInit() –
是的,然后将其分配给一个类变量 – user3642173