【问题标题】:How can i unsubscribe to onAuthStateChanged [duplicate]我如何取消订阅 onAuthStateChanged [重复]
【发布时间】:2017-08-03 09:34:27
【问题描述】:

以前我在firebase.auth.currentUser 上浪费时间,它在应用程序加载时给了我null,并且在应用程序加载时我需要验证用户权限(或者回调也适用于我)。然后我决定使用onAuthStateChanged,但现在我无法取消订阅它并且它一次又一次地触发,我只希望它给我suth并立即永远销毁

那么,我该如何取消订阅这个东西:

firebase.auth().onAuthStateChanged(function(user) {
   if (user) {
       // User is signed in.
   }
});

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    frank 在这里回答了这个问题:Firebase stop listening onAuthStateChanged

    根据文档, onAuthStateChanged() 函数 返回

    观察者的取消订阅功能。所以你可以:

    var unsubscribe = firebase.auth().onAuthStateChanged(function (user) {
        if (user) {
           // User is signed in.
        }
    });
    
    unsubscribe();//this line will unsubscribe the observable :-)
    

    【讨论】:

    • 谢谢! :) 这对我帮助很大。
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    相关资源
    最近更新 更多