angular - 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文档通过弹出窗口登录时,一旦登录完成,函数内的任何内容都应该发生,但它只是重定向到'/'路由。值得注意的是,一旦用户使用弹出窗口登录,我认为页面刷新意味着它只是重新加载到第一条路线。而不是我试图告诉它去的那个。
解决方案
您已经丢失了 的上下文this
,请使用箭头函数:
this.authentication.loginWithGoogle().then((result) => {
if(result.uid !== null) {
this.router.navigate(['/exampleRoute'])
}
})
推荐阅读
- reactjs - 将所有坐标适合地图的边界在地图框中做出反应
- github - 使用具有特定分支的 github ftp 部署操作
- linear-algebra - 特征库是否执行高斯归约以解决齐次系统?
- apache-kafka - 我无法在 Kafka 集群中进行 Kerberos 身份验证
- c# - System.IO.IOException: '参数不正确。' 当 NamedPipeClientStreamReadMode = PipeTransmissionMode.Message
- python - 如何在 imap_tools python 中使用“mark_seen = False”将电子邮件标记为已查看?
- mongodb - docker 中的 quantaxis 无法使用本地化的 mongodb 进行身份验证
- c++ - 猜对数后无法退出while循环
- reactjs - 无需在所有路由中导入的全局 nextjs 中间件
- c# - 在 C# 中灵活编辑 json 值