以前我在 firebase.auth.currentUser
上浪费时间,它在应用程序加载时给我 null
,而我在应用程序加载时需要授权用户(或回调也会为我工作)。然后我决定使用 onAuthStateChanged
但现在我无法取消订阅它并且它一次又一次地触发,我只希望它立即给我 suth 并永远销毁
那么,我怎样才能取消订阅这个东西:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
}
});
frank 在这里回答了这个问题:Firebase stop listening onAuthStateChanged
According to the documentation, the onAuthStateChanged() function
returns
The unsubscribe function for the observer. So you can just:
var unsubscribe = firebase.auth().onAuthStateChanged(function (user) {
if (user) {
// User is signed in.
}
});
unsubscribe();//this line will unsubscribe the observable :-)