NgRx 效果中有条件的 return 动作
Conditionally return actions in NgRx effect
我正在尝试检查效果中的条件并根据条件结果分派不同的操作。条件参数是从 Store 和 API 调用中获得的,如下所示。
@Effect()
// error here - Observable<void> is not assignable to type Observable<Action>
loadUserSkills$: Observable<Action> = this.actions$
.ofType(UserActionTypes.REQUEST_USER_SKILLS)
.withLatestFrom(this.store$)
.map(([action, storeState]) => {
this.skillsService.getUserSkillsCacheVersion().
map((cacheVersion) => {
if (cacheVersion === storeState.users.cacheVersion.userSkills) {
return new UsersActions.RequestUserSkillsFromStore();
} else {
return new UsersActions.RequestUserSkillsFromServer();
}
});
});
这里哪里不对?
我会在不为 loadUserSkills$ 声明类型的情况下尝试这个我从未见过有人在那里声明类型。新标准也不是点链而是使用。
this.actons$.pipe(ofType(...),withLatestFrom(...),...
您是否将商店包含在 class 的构造函数中?
constructor(private store$: AppState, private actions$: Action)
通常您也会在效果中使用 select 或 select 状态片段,而不是像那样直接访问它们。查看 NGRX select 或。
this.store$.pipe(select(selectUserSkills))
但您必须设置一个 select 或称为 selectUserSkills
此外,如果您的 this.skillsService.getUserSkillsCacheVersion() 正在返回一个 Observable,您需要订阅它才能发生任何事情。
我认为这里需要更多信息。请 post 更多代码。此错误是在 运行 时间还是在编译期间发生?
您没有返回地图内部。
return this.skillsService...
所以 typescript 认为它是空的
我通过稍微修改效果来修复它,然后 Fork Joined the observable 如下所示。
@Effect()
loadUserSkills$: Observable<Action> = this.actions$
.ofType(UserActionTypes.REQUEST_USER_SKILLS)
.withLatestFrom(this.store$)
.map(([, storeState]) => storeState.users.cacheVersion.userSkills)
.switchMap((currentCacheVersion) =>
forkJoin([of(currentCacheVersion), this.skillsService.getUserSkillsCacheVersion()]))
.map((data: any) => {
const [currentCacheVersion, latestCacheVersion] = data;
if (latestCacheVersion !== 0 && currentCacheVersion === latestCacheVersion) {
return new UsersActions.RequestUserSkillsFromStore();
} else {
return new UsersActions.RequestUserSkillsFromServer();
}
});
我正在尝试检查效果中的条件并根据条件结果分派不同的操作。条件参数是从 Store 和 API 调用中获得的,如下所示。
@Effect()
// error here - Observable<void> is not assignable to type Observable<Action>
loadUserSkills$: Observable<Action> = this.actions$
.ofType(UserActionTypes.REQUEST_USER_SKILLS)
.withLatestFrom(this.store$)
.map(([action, storeState]) => {
this.skillsService.getUserSkillsCacheVersion().
map((cacheVersion) => {
if (cacheVersion === storeState.users.cacheVersion.userSkills) {
return new UsersActions.RequestUserSkillsFromStore();
} else {
return new UsersActions.RequestUserSkillsFromServer();
}
});
});
这里哪里不对?
我会在不为 loadUserSkills$ 声明类型的情况下尝试这个我从未见过有人在那里声明类型。新标准也不是点链而是使用。
this.actons$.pipe(ofType(...),withLatestFrom(...),...
您是否将商店包含在 class 的构造函数中?
constructor(private store$: AppState, private actions$: Action)
通常您也会在效果中使用 select 或 select 状态片段,而不是像那样直接访问它们。查看 NGRX select 或。
this.store$.pipe(select(selectUserSkills))
但您必须设置一个 select 或称为 selectUserSkills
此外,如果您的 this.skillsService.getUserSkillsCacheVersion() 正在返回一个 Observable,您需要订阅它才能发生任何事情。
我认为这里需要更多信息。请 post 更多代码。此错误是在 运行 时间还是在编译期间发生?
您没有返回地图内部。
return this.skillsService...
所以 typescript 认为它是空的
我通过稍微修改效果来修复它,然后 Fork Joined the observable 如下所示。
@Effect()
loadUserSkills$: Observable<Action> = this.actions$
.ofType(UserActionTypes.REQUEST_USER_SKILLS)
.withLatestFrom(this.store$)
.map(([, storeState]) => storeState.users.cacheVersion.userSkills)
.switchMap((currentCacheVersion) =>
forkJoin([of(currentCacheVersion), this.skillsService.getUserSkillsCacheVersion()]))
.map((data: any) => {
const [currentCacheVersion, latestCacheVersion] = data;
if (latestCacheVersion !== 0 && currentCacheVersion === latestCacheVersion) {
return new UsersActions.RequestUserSkillsFromStore();
} else {
return new UsersActions.RequestUserSkillsFromServer();
}
});