ReactJS - 无法在新路由器上读取未定义的属性“位置”

时间:2017-03-15 22:39:22

标签: javascript reactjs react-router

所以我的index.js文件有一个非常基本的设置,我的所有路由都被保存。我从控制台得到了上面的TypeError,但我不确定为什么。以下是我的代码:

const React = require('react');
const ReactDOM = require('react-dom');

// React router goodies
import { Router, Route, browserHistory, IndexRoute, hashHistory } from 'react-router';

const App = require('./components/App.jsx');

ReactDOM.render(
  <Router history = {browserHistory}> 
    <Route path="/" component={App}>
    </Route>
  </Router>,
  document.getElementById('app')
);

1 个答案:

答案 0 :(得分:5)

react-router版本4不再支持{p> browserHistory,此处提出了问题 - Can no longer import { IndexRoute, browserHistory } from version 4.

切换到版本3.0.0或使用BrowserRouter进行修复。

对于版本3.0.0

"dependencies": {
    "react": "^15.4.1",
    "react-dom": "^15.4.1",
    "react-router": "^3.0.0",
    "webpack": "^1.14.0",
    "webpack-dev-server": "^1.16.2"
}

对于版本4.0.0

const React = require('react');
const ReactDOM = require('react-dom');
import { BrowserRouter, Route } from 'react-router-dom';

const App = require('./components/App.jsx');

ReactDOM.render(
  <BrowserRouter> 
    <Route path="/" component={App}>
    </Route>
  </BrowserRouter>,
  document.getElementById('app')
);