TypeError: action$.ofType(...).mergeMap is not a function

TypeError: action$.ofType(...).mergeMap is not a function

我是 reactjs 的新手,正在尝试将 redux 与我现有的项目集成。

这是我的 index.js 存储中的文件

import 'rxjs'
import { createStore, combineReducers, applyMiddleware } from 'redux'
import { reducer as formReducer } from 'redux-form'
import thunk from 'redux-thunk'
import promise from 'redux-promise-middleware'
import { createEpicMiddleware, combineEpics } from 'redux-observable'

import user, { userEpic } from './user/duck'
import app from './app'

// Bundling Epics
const rootEpic = combineEpics(
    userEpic
)

// Creating Bundled Epic
const epicMiddleware = createEpicMiddleware(rootEpic)

// Define Middleware
const middleware = [
  thunk,
  promise(),
  epicMiddleware
]

// Define Reducers
const reducers = combineReducers({
  app,
  user,
  form: formReducer
})

// Create Store
export default createStore(reducers,window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(), applyMiddleware(...middleware))

这里是duck.js

const createUserEpic = (action$) =>
  action$
  .ofType(SIGNUP_CONCIERGE)
  .mergeMap((action) => {
    return Rx.Observable.fromPromise(api.signUpConcierge(action.payload))
    .flatMap((payload) => ([{
      type: SIGNUP_CONCIERGE_SUCCESS,
      payload
    }]))
    .catch((error) => Rx.Observable.of({
      type: SIGNUP_CONCIERGE_ERROR,
      payload: { error }
    }))
  })

export const userEpic = combineEpics(
  createUserEpic
)

这让我出错 TypeError: action$.ofType(...).mergeMap is not a function

自从我更新了 react、react-redux、redux-observable 版本以来,我一直收到这个错误。

我在这里做错了什么?请帮忙!!!

试试这个:

首先,在文件的最顶部导入这些函数

import { mergeMap } from 'rxjs/operators';
import { ofType } from 'redux-observable';

然后,像这样修正你的代码(注意,ofType()mergeMap() 是由 comma 分隔的,而不是 dot):

const createUserEpic = action$ =>
  action$.pipe(  //fixed
    ofType(SIGNUP_CONCIERGE),
    mergeMap(action => {
      return Rx.Observable.fromPromise(api.signUpConcierge(action.payload))
        .flatMap(payload => [
          {
            type: SIGNUP_CONCIERGE_SUCCESS,
            payload
          }
        ])
        .catch(error =>
          Rx.Observable.of({
            type: SIGNUP_CONCIERGE_ERROR,
            payload: { error }
          })
        );
    })
  );

export const userEpic = combineEpics(createUserEpic);

您忘记了 pipe() 方法,也忘记了从适当的包中导入 ofTypemergeMap 方法。

导入这些方法后,为了使用它们,首先需要像这样使用pipe()方法:

 action$.pipe();

之后,您将可以使用 ofType()mergeMap() 方法:

 action$.pipe(
     ofType(),
     mergeMap()
 );

请注意,它们被 comma 分隔,而不是 dot

根据 this github issue,每个 rxjs 运算符都应在使用前包含。

并且人们建议您是否 import rxjs 在您的 index.js 文件(不是 store/index.js 而是您的项目入口文件)。

或者您可以在 duck.js 中执行 import rxjs/add/operator/mergeMap

两种方式都行,选择哪种方式取决于您。