AngularFire 2 - Auth.logout() 回调

AngularFire 2 - Auth.logout() callback

我正在尝试注销然后导航到登录 url,但是来自此 url 的 authguard 阻止登录用户查看它并且因为在承诺之前到达第二行已解决,您需要单击方法事件两次才能使其正常工作。

logout(){
    this.angularfire.auth.logout();
    this.router.navigate(['']);
  }

有没有办法在 promise 解决后在回调中实现 router.navigate?我试过 then(),但要么我的语法不正确,要么 auth 类型有问题...

AngularFire2 的 logout 应该以类似于底层 Firebase SDK 的方式运行。幸运的是,几天前,a PR was mergedlogout 更改为 return 一个承诺 - 因此下一个版本将解决您的问题。

到那时,您可以收听 auth 更改以确定何时可以导航:

import "rxjs/add/operator/filter";
import "rxjs/add/operator/first";

...

logout(){
  this.angularfire.auth
    // You only want unathenticated states:
    .filter((authState) => !authState)
    // You only want the first unathenticated state:
    .first()
    // You should now be able to navigate:
    .subscribe(() => this.router.navigate(['']));
    // The composed observable completes, so there's no need to unsubscribe.
  this.angularfire.auth.logout();
}

现在已替换为 signOut(),其中 returns 是一个 firebase 承诺。所以如果你导入 import { AngularFireAuth } from 'angularfire2/auth';

然后你就可以

constructor(public afA: AngularFireAuth){}

logout(){
    this.afA.auth.signOut().then(() => {
       this.router.navigate(['']);
    });
}