2016-12-28 40 views
2

我正在嘗試註銷,然後導航到登錄URL,但來自此URL的authguard會阻止登錄用戶查看它,並且自承諾解決之前到達第二行您需要單擊方法事件兩次才能使其工作。AngularFire 2 - Auth.logout()回調

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

有沒有一種方法可以在解決promise的時候在回調中實現router.navigate?我嘗試過然後(),但是或者我的語法不正確或者存在驗證類型問題...

+1

angularfire.auth.logout()函數是無效的,不返回任何東西,你爲什麼不能再使用()。 –

回答

5

AngularFire2的logout的行爲應該與底層的Firebase SDK的行爲類似。幸運的是,前幾天,a PR was merged更改logout以返回承諾 - 所以下一個版本將解決您的問題。

在那之前,你可以聽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(); 
} 
+0

是啊...我讀了SDK,這就是爲什麼我認爲它返回了一個承諾。謝謝! – cerealex

0

這已經被替換爲signOut()它返回一個火力點承諾。所以,如果你輸入import { AngularFireAuth } from 'angularfire2/auth';

然後你可以

constructor(public afA: AngularFireAuth){} 

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