我尝试使用react-router-dom 4.0.0库.但它发给我这个错误
Uncaught TypeError: Cannot read property ‘location’ of undefined
这似乎是browserHistore中的问题.在我使用react-router 2.x.x之前,一切都没问题.
这是我的index.js
import 'babel-polyfill' import React from 'react' import { Router,hashHistory } from 'react-router-dom' import { render } from 'react-dom' import { Provider } from 'react-redux' import { configureStore } from './store' import { routes } from './routes' const store = configureStore() render( <Provider store={store}> <Router history={hashHistory} routes={routes} /> </Provider>,document.getElementById('root') )
这是我的路线
import React from 'react' import { IndexRoute,Route } from 'react-router-dom' import App from './containers/App' import Main from './containers/Main' import First from './containers/First' export const routes = ( <Route path='/' component={Main}> <Route path='/path' component={First} /> <IndexRoute component={App} /> </Route> )
而且对于服务器端快递我设置了这个get配置
app.get('*',function root(req,res) { res.sendFile(__dirname + '/index.html'); });
解决方法
React Router v4是一个完整的重写,并且与您之前的版本不兼容,正如您在代码中所假设的那样.话虽如此,您不应期望能够升级到新的主要版本(V4)并让您的应用程序正常工作.您应该查看
documentation或降级回V2 / 3.这里有一些代码可以帮助您开始正确的方向
import 'babel-polyfill' import React from 'react' import { BrowserRouter as Router,Route } from 'react-router-dom' import { render } from 'react-dom' import { Provider } from 'react-redux' import { configureStore } from './store' import App from './containers/App' import Main from './containers/Main' import First from './containers/First' const store = configureStore() render( <Provider store={store}> <Router> <Route path='/' component={Main} /> <Route path='/path' component={First} /> </Router> </Provider>,document.getElementById('root') )