Firebase 停止监听 onAuthStateChanged [英] Firebase stop listening onAuthStateChanged
问题描述
从 ^3.0.0 版本开始,我很难删除身份验证状态更改侦听器.
As of version ^3.0.0, I'm having a difficult time removing the auth state change listener.
根据文档启动侦听器:
firebase.auth().onAuthStateChanged(function (user) {
// handle it
});
但是,我在文档中找不到任何提到删除身份验证状态更改侦听器的地方.Firebase.Auth 类上有一个名为 removeAuthTokenListener
的特殊函数.不幸的是,它没有记录(firebase 文档参考).
However, I cannot find anywhere in the documentation that refers to a remove auth state change listener. There is peculiar function on the Firebase.Auth class called removeAuthTokenListener
. Unfortunately it's not documented (firebase docs reference).
通过浏览器的网络控制台.
Via your browser's web console.
var auth = firebase.auth();
auth.removeAuthTokenListener;
打印带有一个参数的函数定义.我尝试执行以下操作:
prints a function definition that takes one parameter. I tried to do the following:
this.authListener = firebase.auth().onAuthStateChanged(function (user) {...});
firebase.auth().removeAuthTokenListener(this.authListener);
但这并没有起到任何作用.
but that didn't do anything.
推荐答案
根据 文档,onAuthStateChanged()
函数返回
观察者的取消订阅功能.
The unsubscribe function for the observer.
所以你可以:
var unsubscribe = firebase.auth().onAuthStateChanged(function (user) {
// handle it
});
然后:
unsubscribe();
这篇关于Firebase 停止监听 onAuthStateChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!