2

Redux 路由器版本 ^2.1.2

反应路由器版本: "react-router": "^3.0.0",历史版本:"history": "^4.3.0"

重现步骤

    import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import { Router, browserHistory } from 'react-router';
import { reduxReactRouter, routerStateReducer, ReduxRouter } from 'redux-router';
import reduxThunk from 'redux-thunk';
import { createHistory } from 'history';

import rootReducer from './core/rootReducer';
import routes from './core/routes';

import * as axios from './utils/axios.js';

import { AUTH_USER } from './authentication/types';

const store = compose(applyMiddleware(reduxThunk),
    reduxReactRouter({ routes, createHistory })
)(createStore)(rootReducer);

const token = localStorage.getItem('token');

if (token) {
    store.dispatch({ type: AUTH_USER });
}

ReactDOM.render(
    <Provider store={store}>
        <ReduxRouter>

            {routes}

        </ReduxRouter>
    </Provider>
    , document.querySelector('.main')
);

它返回一个错误index.js:34416 Uncaught TypeError: createHistory is not a function(…)

有什么解决办法吗?


改成import { createHistory } from 'history';之后import createHistory from 'history/createBrowserHistory';

错误变成Uncaught TypeError: history.getCurrentLocation is not a function

想法?

4

1 回答 1

5

我通过在 package.json 中指定历史版本 3 来解决此问题

"history": "^3.0.0",

react-router 3.0.0 将历史 3.0.0 指定为依赖项,但由于某种原因,它没有添加到 node_modules 中。但是将它显式添加到您自己的 package.json 为我解决了这个问题。

我不认为 react-router 3.0.0 与 history v4 兼容。我不确定为什么 npm 没有观察到 react-router v3.0.0 package.json 中的依赖关系

于 2016-12-11T08:07:50.607 回答