我想让用户保持登录状态(当他关闭页面时,他不需要再次登录。根据文档,我正在执行以下操作:
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.
我显然正在使用它。不知何故,我无法兑现signInWithEmailAndPassword的承诺。
有帮助吗?谢谢
答案 0 :(得分:2)
在第一个然后调用(在... 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的更多信息。