gpt4 book ai didi

angular - 如何使用带有参数的选择器从效果中的ngrx存储中选择

转载 作者:行者123 更新时间:2023-12-03 15:36:15 28 4
gpt4 key购买 nike

我需要构建一个效果,并且需要从存储中获取一个值,问题是选择器是带有参数的选择器。

遵循示例代码:

        @Effect()
ExampleEffect$ = this.actions$.pipe(
ofType(
ActionTypes.SOMETHING
),
map((action: Somthing) => action.payload.myParameter),
// HERE I NEED THE PARAMETER TO PERFROM THE SELECTION
withLatestFrom(this.store.pipe(select(selectorWithParamter(myParameter))),
map((value) => /* do somthing with the array [myParameter, valueSelected from sotre]*/)

最佳答案

您可以在创建选择器时编写箭头函数来传递参数。

export const getAlbumById = (collectionId: number) => createSelector(getAlbumEntities, entities => entities[collectionId]);

更多例子

//影响
@Effect({ dispatch: true })
upsertAlbum$ = this.actions$.pipe(
ofType(AlbumActionTypes.UpsertAlbum),
map((action: any) => action.payload),
mergeMap(({ album }) =>
this.store.pipe(
select(selectAlbumIfExists(album.id)),
first(),
map(isAlbumHasName => [album, isAlbumHasName])
)
),
filter(([album, isAlbumHasName]) => !isAlbumHasName),
map(([album]) => new LoadAlbum({ album }))
);

//选择器
export const selectAlbumIfExists = (id: string) =>
createSelector(
selectAlbumsEntities,
entities => !!(entities[id] && entities[id].name)
);

关于angular - 如何使用带有参数的选择器从效果中的ngrx存储中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53519731/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com