【问题标题】:return for SwitchMap in Rxjs - Angular在 Rxjs 中返回 SwitchMap - Angular
【发布时间】:2019-05-22 22:23:50
【问题描述】:

我是第一次使用 switchMap。它告诉我要返回“某物”,但是当我返回时它不起作用。

 @Effect()
  subData$ = this.actions$.pipe(
    ofType(ActionTypes.SEARCH_SUB_DATA),
    map(action => action['payload']),
    switchMap(payload => { //Payload gets highlighted in red
      const repos = this.githubSearch.getRepos(payload);
      const followers = this.githubSearch.getFollowers(payload);
      const starred = this.githubSearch.getStarred(payload);
      return forkJoin([repos, followers, starred]).subscribe(results => {
        this.store.dispatch(
          new UserActions.SuccessSubData({
            user: payload,
            repos: results[0],
            followers: results[1],
            starred: results[2]
          })
        );
        return of(results);
      });
    })
  );

'(payload: never) => Subscription' 类型的参数不可赋值 到类型参数'(值:从不,索引:数字)=> 可观察输入'。类型“订阅”不可分配给类型 'ObservableInput'。

根据提供的答案进行更新

@Effect()
      subData$ = this.actions$.pipe(
        ofType(ActionTypes.SEARCH_SUB_DATA),
        map(action => action['payload']),
        switchMap(payload => {
          return forkJoin(
            this.githubSearch.getRepos(payload),
            this.githubSearch.getFollowers(payload)
          );
        }).subscribe(results => {
          this.store.dispatch(
            new UserActions.SuccessSubData({
              user: payload,
              repos: results[0],
              followers: results[1]
            })
          );
        })
      );

这是显示错误的图片

image from error

【问题讨论】:

    标签: angular rxjs observable


    【解决方案1】:
    @Effect()
      subData$ = this.actions$.pipe(
        ofType(ActionTypes.SEARCH_SUB_DATA),
        map(action => action['payload']),
        switchMap(payload => {
          return forkJoin(
            this.githubSearch.getRepos(payload),
            this.githubSearch.getFollowers(payload)
          ).pipe(map(([repos,followers]) => //return success action here), catchError(error => of(//return failaction))
        })
      )
    

    通常您不必订阅,它只会返回流并让ngrx处理订阅a和取消订阅。所以在这里我正在映射成功和失败回调操作。它们会被 ngrx 自动触发。

    【讨论】:

    • 很高兴你发现它有用。
    【解决方案2】:

    您正在尝试将ObservableSubscription 合并,并且在使用forkJoin 时,您正在使用两个undefined 值。下面的代码必须适用于您的情况。

    @Effect()
      subData$ = this.actions$.pipe(
        ofType(ActionTypes.SEARCH_SUB_DATA),
        map(action => action['payload']),
        switchMap(payload => {
          return forkJoin(
            this.githubSearch.getRepos(payload),
            this.githubSearch.getFollowers(payload)
          );
        })
      ).subscribe(results => {
          this.store.dispatch(
            new UserActions.SuccessSubData({
              user: payload,
              repos: results[0],
              followers: results[1]
            })
          );
        });
    

    【讨论】:

    • 你好!谢谢您的帮助。这没有用。没有每个单词 'payload' things 都是一个对象而不是一个字符串,.subscribre() 是红色的,因为显然 switchmap 不会返回一个 observable
    • 是的,我的错,我编辑了答案,我很确定它现在必须工作。
    • 还是同样的问题。 Idk 如果我在效果内有 switchMap 会有所不同。我已经用你的代码更新了我的问题并附上了一张图片
    • 我刚刚又编辑了,原来你已经去掉了forkJoin中的括号。
    • 同样的错误 :( 我已经用没有括号和更新图像的代码更新了我的问题
    猜你喜欢
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2018-02-22
    • 1970-01-01
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多