代码之家  ›  专栏  ›  技术社区  ›  AlbertMunichMar

标记的角度+火力基础持续性

  •  0
  • AlbertMunichMar  · 技术社区  · 6 年前

    我想让用户登录持久性(当他关闭页面时,不需要再次登录)。根据文档,我正在执行以下操作:

    signinUser(email: string, password: string) {
        firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
            .then(function() {
                return firebase.auth().signInWithEmailAndPassword(email, password)
                    .then(
                        response => {
                            console.log(response);
                            this.router.navigate(['/']);
                        }
                    );
            })
            .catch(function(error) {
                // Handle Errors here.
                var errorCode = error.code;
                var errorMessage = error.message;
            });
    }
    

    但我不是在航行。奇怪的是,我注入了路由器:

    constructor(private router: Router) {}
    

    它告诉我:

    [ts] Property 'router' is declared but its value is never read.
    

    很明显我在用它。不知何故,我无法兑现没有电子邮件和密码登录的承诺。

    有什么帮助吗?谢谢

    1 回复  |  直到 6 年前
        1
  •  2
  •   Adrian Fâciu    6 年前

    在第一个然后调用(persistence.local之后的那个)中,有一个函数声明。与箭头函数不同,它不会保留当前上下文,因此当您使用 this.router ,请 将是上述定义函数的上下文。

    因此警告是正确的,您没有使用类中定义的路由器,而是在函数上下文中使用未定义的路由器属性。你可能看不到任何错误,因为那时内在没有捕捉。

    解决此问题的最快方法是在所有位置使用箭头函数,例如:

    signinUser(email: string, password: string) {
        firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
            .then(() => firebase.auth().signInWithEmailAndPassword(email, password)
                    .then(
                        response => {
    ...
    

    你可以找更多关于 this here 是的。