Ngrx Effects withLatestFrom 导致异常
Ngrx Effects withLatestFrom causes an exception
我正在尝试调度一个将根据所选出版商加载图书的操作。
这是效果示例class:
@Injectable()
export class LibraryEffects {
@Effect({})
bookRequest = this.actions.pipe(
ofType(BOOK_LIST_REQUEST),
withLatestFrom(
this.store.pipe(
select(selectBookshop),
map(bookshop => bookshop.library.publisher),
)
),
map(([action, publisher]) => {
window.console.log(action, publisher);
return new BookRequestSuccess();
})
);
constructor(
private actions: Actions,
private store: Store<AppState>
) {}
}
当应用程序启动时,存储中不包含 'bookshop' 也不包含 'library' 属性,这些属性稍后会被用户操作填充。在该用户触发 'BOOK_LIST_REQUEST' 操作后,应该检索适当的图书列表。
但是即使没有调用应该订阅 'library' 属性 存储中的 'BOOK_LIST_REQUEST' 操作,但一旦注册效果就会发生异常:
core.js:15714 ERROR TypeError: Cannot read property 'library' of null
这看起来很奇怪,好像 effect 是在模块中注册一个 observable,而不是调用 'BOOK_LIST_REQUEST' 操作。
这里可能有什么问题?我在这种情况下从存储中读取是否有某种错误?
如果 withLatestFrom 像在 ngrx documentation example (collection.effects.ts).
中那样被包裹在 concatMap 运算符中,这个问题似乎已得到解决
ofType(BOOK_LIST_REQUEST),
concatMap(action => of(action).pipe(
withLatestFrom(this.store.pipe(
select(selectBookshop),
map(bookshop => bookshop.library.publisher)
)
)
我正在尝试调度一个将根据所选出版商加载图书的操作。
这是效果示例class:
@Injectable()
export class LibraryEffects {
@Effect({})
bookRequest = this.actions.pipe(
ofType(BOOK_LIST_REQUEST),
withLatestFrom(
this.store.pipe(
select(selectBookshop),
map(bookshop => bookshop.library.publisher),
)
),
map(([action, publisher]) => {
window.console.log(action, publisher);
return new BookRequestSuccess();
})
);
constructor(
private actions: Actions,
private store: Store<AppState>
) {}
}
当应用程序启动时,存储中不包含 'bookshop' 也不包含 'library' 属性,这些属性稍后会被用户操作填充。在该用户触发 'BOOK_LIST_REQUEST' 操作后,应该检索适当的图书列表。
但是即使没有调用应该订阅 'library' 属性 存储中的 'BOOK_LIST_REQUEST' 操作,但一旦注册效果就会发生异常:
core.js:15714 ERROR TypeError: Cannot read property 'library' of null
这看起来很奇怪,好像 effect 是在模块中注册一个 observable,而不是调用 'BOOK_LIST_REQUEST' 操作。
这里可能有什么问题?我在这种情况下从存储中读取是否有某种错误?
如果 withLatestFrom 像在 ngrx documentation example (collection.effects.ts).
中那样被包裹在 concatMap 运算符中,这个问题似乎已得到解决 ofType(BOOK_LIST_REQUEST),
concatMap(action => of(action).pipe(
withLatestFrom(this.store.pipe(
select(selectBookshop),
map(bookshop => bookshop.library.publisher)
)
)