[英]Redux observable epic with Rxjs issue
我的史诗如下
export const fetchSomethingEpic = (action$, state$) => {
return action$.pipe(ofType(someActions.FETCH_LEADS),
mergeMap(action =>
from(api().get('SOMETHING'))
.map(response => fetchSomethingSuccess(response.data))
.catch(error => Observable.of(fetchSomethingError(error))))
);
}
api这里是axios.create
,它发生在不同的文件中
基本上,当我安装了rxjs-compat
时,这种方法非常有效。 但试图删除它,现在给我跟随错误
TypeError: Object(...)(...).map is not a function
安装了rxjs-compat
,它的工作原理
我有
import { Observable } from 'rxjs/Rx';
import { mergeMap } from 'rxjs/operators';
"rxjs": "6.2.1",
"rxjs-compat":"6.2.1"
我现在有
import { Observable } from 'rxjs'
import { mergeMap, map} from 'rxjs/operators';
"rxjs": "^6.5.2"
我在这里错过了一些东西
您必须使用pipe
传递observable才能使用map
运算符。 还要导入catchError
来处理错误。
export const fetchSomethingEpic = (action$, state$) => {
return action$.pipe(ofType(someActions.FETCH_LEADS),
mergeMap(action =>
from(api().get('SOMETHING'))
.pipe(map(response => fetchSomethingSuccess(response.data)),
catchError(error => Observable.of(fetchSomethingError(error))))
);
}
解决方法如下
export const fetchSomethingEpic = (action$) =>
action$.ofType(someActions.FETCH_LEADS)
.pipe(mergeMap((action) => from(api().get('SOMETHING'))
.pipe(map(response => fetchSomethingSuccess(response.data)),
catchError(error => fetchSomethingError(error))))
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.