Angularfire在signInWithPopup()之后无法重定向

时间:2018-06-21 16:18:31

标签: angular firebase firebase-authentication angularfire2

使用Angular2和angularfire2我正在尝试使用“ signInWithPopup”功能登录到Google后重定向用户,一旦用户在页面中登录后立即刷新并加载首页,但是我想要它登录后将用户重定向到另一个页面。

以下代码是将用户登录到Google的功能。

 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文档通过弹出窗口登录时,一旦登录完成,该函数内的任何内容都应发生,而只是重定向到“ /”路由。值得注意的是,一旦用户使用弹出窗口登录,我认为页面会刷新,这意味着它会重新加载到第一条路线。而不是我要告诉它去的那个。

1 个答案:

答案 0 :(得分:2)

您丢失了this的上下文,请使用箭头功能:

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