angular - Angular路由器很慢
问题描述
我在引擎盖下使用 Ionic 和 Angular。
设置
"@angular/router": "~9.1.9",
"@ionic/angular": "^5.1.1",
"rxjs": "^6.5.5",
我使用以下登录方法:当它被触发时,微调器被显示,数据被加载,路由到另一个页面,然后微调器再次被隐藏。
loginWithGoogle() {
this.showSpinner = true;
this.authService.getAuthSessionPersistence()
.pipe(
concatMap(() => this.authService.getAuth().signInWithPopup(new auth.GoogleAuthProvider())),
concatMap(() => this.databaseService.getUserModel()
.pipe(
concatMap(userModel => {
if (Object.keys(new UserInit()).every(key => userModel[key])) {
return of(userModel).pipe(first());
}
return this.authService.getUser()
.pipe(
concatMap(user => this.databaseService.createUser(user.uid, userModel))
);
}),
concatMap(userModel => this.store.dispatch([
new SaveUserAction(userModel),
new LoadFixturesAction(),
new LoadTeamsAction(),
new LoadTransactionsAction(),
])
),
tap(() => this.router.navigateByUrl('tabs/home'))
)),
finalize(() => this.showSpinner = false)
)
.subscribe();
}
问题是微调器在路由发生之前就被隐藏了。用户再次看到不太友好的登录屏幕。
正如你在上面的代码中看到的: 最后一个 RxJS 操作符是 use finalize
,它是微调器被隐藏的地方。
你知道为什么路由器需要很长时间才能导航吗?
解决方案
感谢我们设法找到解决方案的答案。
错误
tap(() => this.router.navigateByUrl('tabs/home'))
- this.router.navigateByUrl('tabs/home') 返回一个承诺
- 点击会产生副作用并且不等待承诺解决
解决方案
concatMap(() => from(this.router.navigateByUrl('tabs/home')))
- from() 将 promise 转换为 observable
- concatMap() 等待 observable
推荐阅读
- node.js - 使用 .createServer 制作公共网站
- postgresql - pgadmin4 指定的用户不存在
- c++ - 如何以二进制模式读取并将信息传输到新文件
- c++ - 多态类的typeid,当给定一个派生类的指针时,说它是一个指向基类的指针!为什么?
- java - 模拟返回错误的集合
- python-3.x - Python 日志记录 - 从配置文件配置旋转文件处理程序
- javascript - 汉堡按钮显示不变
- jenkins - 如何为 Jenkins 上的 TestCafe 测试创建通过阈值
- java - 适用于不同环境的 Azure JAVA Functions + DevOps 管道
- python - 为什么我的电脑安装不上tensorflow 1.13.1版本?