这是我第一次使用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]
})
);
})
);
下面的图片显示了错误
发布于 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自动触发。
发布于 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]
})
);
});
https://stackoverflow.com/questions/56265751
复制相似问题