Firebase停止侦听AuthStateChanged [英] Firebase stop listening onAuthStateChanged

查看:132
本文介绍了Firebase停止侦听AuthStateChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从版本^ 3.0.0开始,我很难去除auth状态改变侦听器。



firebase.auth ().onAuthStateChanged(function(user){
//处理
});

但是,我无法找到引用remove auth状态更改侦听器的文档中的任何地方。在Firebase.Auth类中有一个特殊的函数叫做 removeAuthTokenListener 。不幸的是,它没有记录( Firebase文档参考)。

通过浏览器的网络控制台。

  var auth = firebase.auth(); 
auth.removeAuthTokenListener;

打印一个带有一个参数的函数定义。我试图做到以下几点:$ b​​
$ b $ p $ this.authListener = firebase.auth()。onAuthStateChanged(function(user){.. 。});
firebase.auth()。removeAuthTokenListener(this.authListener);

但是没有做任何事情。根据 auth.Auth#onAuthStateChanged>文档 onAuthStateChanged()函数返回


观察者的取消订阅功能。


所以你可以:

  var unsubscribe = firebase.auth()。onAuthStateChanged(function(user){
//处理
});

然后:

 退订(); 


As of version ^3.0.0, I'm having a difficult time removing the auth state change listener.

To start the listener per the documentation:

firebase.auth().onAuthStateChanged(function (user) {
    // handle it
});

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.

解决方案

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) {
    // handle it
});

And then:

unsubscribe();

这篇关于Firebase停止侦听AuthStateChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆