首页 > 解决方案 > 在服务(ngOnInit 或构造函数内部)中获取数据的确切位置?

问题描述

在从我的离子角度应用程序注销时,我正在使用 auth.signOut() 方法。但是,当我注册新电子邮件时,在主页组件中,它正在从先前已签出的用户那里获取数据。看起来服务的构造函数没有被调用。当我重新加载时,它工作正常。

constructor(
private authService: AuthService,
private db: AngularFireDatabase,
) {
this.getUserId();
this.patientsRef = db.list(`/${this.loggedUserId}/patients`);
this.afPatients = this.patientsRef.snapshotChanges()
    .pipe(
      map(changes =>
        changes.map(c => ({ id: c.payload.key, ...c.payload.val()}))
        )
    );
} 

getUserId() {
this.authSub = this.authService.userId
.pipe(
  take(1)
).subscribe(userId => {
  if (!userId) {
    return;
  } else {
    this.loggedUserId = userId.toString();
  }
});
} 

标签: angularfirebaseionic-frameworkrxjsfirebase-authentication

解决方案


您可以在auth.signOut(). 如果您提供示例代码也会更好。


推荐阅读