Angular 返回Rxjs中的开关映射-角度
我第一次使用switchMap。它告诉我归还“某物”,但当我归还时,它不起作用Angular 返回Rxjs中的开关映射-角度,angular,rxjs,observable,Angular,Rxjs,Observable,我第一次使用switchMap。它告诉我归还“某物”,但当我归还时,它不起作用 @Effect() subData$ = this.actions$.pipe( ofType(ActionTypes.SEARCH_SUB_DATA), map(action => action['payload']), switchMap(payload => { //Payload gets highlighted in red const repos =
@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);
});
})
);
类型为“(有效负载:从不)=>Subscription”的参数不可分配
至类型为“”的参数(值:从不,索引:编号)=>
可观察输入'。类型“订阅”不可分配给类型
“可观测输入”
根据提供的答案进行更新
@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]
})
);
})
);
这是显示错误的图片
您试图将一个
可观察的与一个订阅合并,并且在使用forkJoin
时,您正在使用两个未定义的值。下面的代码必须适用于您的案例
@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]
})
);
});
通常你不需要订阅,它只会返回流,让ngrx处理订阅和取消订阅。因此,我在这里映射成功和失败回调操作。它们将由ngrx自动触发。Hola parce!谢谢你的帮助。这不管用。不,每个单词“payload”都是一个对象,而不是一个字符串,.subscribre()
是红色的,因为aparently switchmap不会返回可观察的值。啊,我的错,我编辑了答案,我很确定它现在一定能工作。仍然是同一个问题。Idk如果我在一个效果里面有一个开关贴图会有不同。我已经用你的代码更新了我的问题,并附上了一张我刚刚编辑过的图片,结果发现你去掉了forkJoin中的括号。同样的错误:(我用没有brakets和udpated的代码更新了我的问题,很高兴你发现它有用。
@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))
})
)