我第一次使用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)=>订阅类型的参数不可分配 到类型'(值:从不,索引:数字)=>的参数 ObservableInput <{}>'。不能将“订阅”类型分配给该类型 '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]
})
);
})
);
这是显示错误的图片
答案 0 :(得分: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处理subscriptiona和unsubscription。所以在这里我映射成功和失败的回调动作。它们将由ngrx自动触发。
答案 1 :(得分:0)
您正在尝试将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]
})
);
});