【问题标题】:How to ignore events in Observable if condition does not match?如果条件不匹配,如何忽略 Observable 中的事件?
【发布时间】:2019-05-14 19:38:59
【问题描述】:

我想放置一个条件,以便在嵌套管道条件中不满足条件时忽略操作事件。

如果我只返回ignoreElements,则会导致返回值不流式传输的错误。

export const getDataEpic: (action$, state$,
    { apolloClient, services }: { apolloClient: ApolloClient<any>, services: any }) => any
    = (action$, state$, { apolloClient, services }) =>
        action$.pipe(ofType('TEST)).pipe(map(x => x))
            .pipe(
                switchMap((action) =>
                    of([true, false, true]))
                        .pipe(
                            map(settings => ({ action, config: settings})), // true or false
                            switchMap(value=> {
                                if(value.config)
                                  return of([1])
                                } else {
                                   // if condition not met then don't emit anything
                                    return ignoreElements();
                                }
                            }),
                        ),

                ),
                groupBy(value => value.action.payload.model.resource),
                mergeMap(ngroup => ngroup

【问题讨论】:

    标签: observable rxjs6


    【解决方案1】:

    错误是因为您返回的是 操作符函数 而不是 observable。如果您需要一个不发出任何项目的 observable,您应该返回 EMPTY。

    import { EMPTY } from 'rxjs'
    
    ...
    switchMap(value => {
      if (value.config)
        return of([1])
      } else {
        // if condition not met then don't emit anything
        return EMPTY
      }
    })
    ...
    

    【讨论】:

    • 啊……明白了。让我测试一下。
    猜你喜欢
    • 2019-06-02
    • 2019-09-11
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多