我在这里使用的是AngularFire2版本的代码,我用它来从我的一个组件上的数据库中获取一些成员。
我在同一个组件中调用此注销时遇到问题。我取消订阅了订阅,但我仍然在控制台中收到错误Exception was thrown by user callback. Error: permission_denied at /users/uid
。我知道这是因为我不再登录,我的数据库规则不允许这种读取操作。如果我已经取消订阅,我不知道为什么它还在尝试读取数据。
constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
this.user = afAuth.authState;
this.userSub = this.user.subscribe(auth => {
if (auth) {
this.member = db.object('/users/' + auth.uid);
this.dbSub = this.member.subscribe();
}
});
}
logout() {
this.dbSub.unsubscribe();
this.userSub.unsubscribe();
this.afAuth.auth.signOut()
.then(() => this.router.navigateByUrl('/login'));
}
答案 0 :(得分:5)
您可以尝试使用RxJS主题以及运算符takeUntil()
来帮助确保在signOut()
之前清除observable:
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/takeUntil';
export class Foo {
private ngUnsubscribe: Subject<void> = new Subject<void>();
constructor(private afAuth: AngularFireAuth, private db: AngularFireDatabase) {
this.user = afAuth.authState;
this.userSub = this.user.subscribe(auth => {
if (auth) {
this.member = db.object('/users/' + auth.uid);
this.dbSub = this.member
.takeUntil(ngUnsubscribe)
.subscribe(results => console.log(results));
}
});
}
logout() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
this.userSub.unsubscribe();
this.afAuth.auth.signOut()
.then(() => this.router.navigateByUrl('/login'));
}
}
希望这有帮助!