首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Rxjs - Angular中返回SwitchMap

在Rxjs - Angular中返回SwitchMap
EN

Stack Overflow用户
提问于 2019-05-23 06:23:51
回答 2查看 171关注 0票数 0

这是我第一次使用switchMap。它告诉我要返回"something",但是当我返回一个return时,它就不起作用了。

 @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);
      });
    })
  );

‘(

:never) => Subscription’类型的参数不能分配给类型为'(value: never,index: number) => ObservableInput<{}>‘的参数。类型'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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-23 10:47:22

@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处理订阅和取消订阅。因此,我在这里映射成功和失败回调操作。它们将由ngrx自动触发。

票数 1
EN

Stack Overflow用户

发布于 2019-05-23 06:30:59

您正在尝试将一个Observable与一个Subscription合并,并且当使用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]
        })
      );
    });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56265751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档