gpt4 book ai didi

angular - 将 max 与选择器一起使用

转载 作者:行者123 更新时间:2023-12-04 08:10:17 24 4
gpt4 key购买 nike

我想编写一个选择器来获取所有问题,获取具有最大顺序的对象并返回此顺序(这是一个数字)。所以我的想法是:我必须选择我所有的问题,获得最大订单并映射这个订单。但我不知道。
选择器:

export const selectQuestions = (state: AppState) => state.questions;
我想写的选择器:
export const getLastOrderIndex = createSelector(
select(selectQuestions),
max<Question>((a: Question, b: Question) => a.order < b.order ? -1 : 1),
map(x => x.order)
)
日志错误(visual studio code 给我):
No overload matches this call.
Overload 1 of 32, '(s1: Selector<Observable<AppState>, Observable<Question>>, s2: Selector<Observable<AppState>, Observable<any>>, projector: (s1: Observable<...>, s2: Observable<...>) => Observable<...>): MemoizedSelector<...>', gave the following error.
Argument of type '(source$: Observable<AppState>) => Observable<QuestionState>' is not assignable to parameter of type 'Selector<Observable<AppState>, Observable<Question>>'.
Type 'Observable<QuestionState>' is not assignable to type 'Observable<Question>'.
Type 'QuestionState' is missing the following properties from type 'Question': id, type, sectionName, order, and 4 more.
Overload 2 of 32, '(s1: SelectorWithProps<Observable<AppState>, unknown, Observable<Question>>, s2: SelectorWithProps<Observable<AppState>, unknown, Observable<...>>, projector: (s1: Observable<...>, s2: Observable<...>, props: unknown) => Observable<...>): MemoizedSelectorWithProps<...>', gave the following error.
Argument of type '(source$: Observable<AppState>) => Observable<QuestionState>' is not assignable to parameter of type 'SelectorWithProps<Observable<AppState>, unknown, Observable<Question>>'.
Type 'Observable<QuestionState>' is not assignable to type 'Observable<Question>'
我知道有些地方很不对,但我不知道在哪里。谁能给我解释一下?我阅读了所有文档,但我知道我错过了一些东西。

最佳答案

我认为您错误地创建了选择器。您正在尝试使用没有可观察的 RxJS 运算符。
相反,您可以使用 reduce 来查找最大值:

export const selectQuestions = (state: AppState) => state.questions;

export const selectMaxOrder = createSelector(
selectQuestions,
(questions: Array<Question>) => questions
.reduce(
(max, cur) => cur.order > max ? cur.order : max,
0
)
);
然后在您的组件/外观中,您可以将选择器用作:
maxOrder$ = this.store.pipe(select(selectMaxOrder));

关于angular - 将 max 与选择器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66009062/

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