首页 > 解决方案 > Angularfire在signInWithPopup()之后无法重定向

问题描述

使用 Angular2 和 angularfire2 我试图在用户使用“signInWithPopup”功能登录谷歌后重定向用户,一旦用户登录页面立即刷新并加载首页,但是我想让它重定向用户登录后转到不同的页面。

下面的代码是将用户登录到谷歌的功能。

 loginWithGoogle() {
return this.authentication.auth
  .signInWithPopup(new firebase.auth.GoogleAuthProvider());}

下面的代码是尝试登录用户的函数,调用上面的函数。

loginWithGoogle() {
this.authentication.loginWithGoogle().then(function (result) {
  this.router.navigate(['/exampleRoute']);
}).catch(function (error) {
  alert('error');
});}

当用户根据firebase文档通过弹出窗口登录时,一旦登录完成,函数内的任何内容都应该发生,但它只是重定向到'/'路由。值得注意的是,一旦用户使用弹出窗口登录,我认为页面刷新意味着它只是重新加载到第一条路线。而不是我试图告诉它去的那个。

标签: angularfirebasefirebase-authenticationangularfire2

解决方案


您已经丢失了 的上下文this,请使用箭头函数:

this.authentication.loginWithGoogle().then((result) => { 
  if(result.uid !== null) {
    this.router.navigate(['/exampleRoute'])
  }
})

推荐阅读