rxjs - 在路由解析器中使用 NGRX
问题描述
我正在使用 Angular 6。我也在使用 NGRX 商店。我正在使用路由保护来确保用户登录到应用程序。然后,我使用解析器获取初始用户配置文件,然后将其放置在 NGRX 存储中。
我是 NGRX 的新手,我不确定这是否是编写解析器的正确方法。
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
return this.loginService.getLoginData()
.pipe(
map((result:UserData) => {
this.store.dispatch(new userActions.SetLoginData(result));
this.loginService.getDropdownData(
result.userId,
result.countryCode,
).subscribe( data => {
this.store.dispatch(new userActions.SetDropdownData(data));
})
})
)
}
我也不确定这是否是执行 RXJS 的正确方法。
任何建议,谢谢
解决方案
我将向您指出Preloading ngrx/store with Route Guards,Todd Motto 的一篇文章很好地解释了它。
NgRx 示例应用程序中还有一个守卫示例
@Injectable()
export class CoursesGuard implements CanActivate {
constructor(private store: Store<CoursesState>) {}
getFromStoreOrAPI(): Observable<any> {
return this.store
.select(getCoursesState)
.do((data: any) => {
if (!data.courses.length) {
this.store.dispatch(new Courses.CoursesGet());
}
})
.filter((data: any) => data.courses.length)
.take(1);
}
canActivate(): Observable<boolean> {
return this.getFromStoreOrAPI()
.switchMap(() => of(true))
.catch(() => of(false));
}
}
推荐阅读
- python - 使用 1D 数组对 3D 数组进行排序
- php - 如何使用magento 2 rest api按商店ID更新产品的基本价格?
- docker - 如何运行创建多个容器并使用 k8s 在内部运行不同的命令
- python - First Fit Bin 打包算法
- git - 如何摆脱合并提交中的错误并保留正确的部分?
- docker - Dockerfile 不会安装 cron
- android - 运行 flutter build appbundle 时的问题 | 无法确定任务“:app:transformClassesAndResourcesWithR8ForRelease”的依赖关系
- python - Discord.py 在嵌入消息中发送 youtube 缩略图
- fasttext - 为什么fastText的文本分类示例没有在标签上应用LabelEncoder
- go - Go 模板 - 范围的语法