2016-11-29 54 views
0

我一直在关注本教程以获得React Router的工作https://scotch.io/tutorials/routing-react-apps-the-complete-guide ......但是当我尝试并利用反应路由器的任何功能时 - 它给我这个:react router - Router.js:111Uncaught TypeError:无法读取未定义的属性'getCurrentLocation'

Router.js:111Uncaught TypeError: Cannot read property 'getCurrentLocation' of undefined at Object.createTransitionManager (localhost:8080/public/bundle.js:22730:14) at Object.componentWillMount (localhost:8080/public/bundle.js:22737:36) at localhost:8080/public/bundle.js:15709:24 at measureLifeCyclePerf (localhost:8080/public/bundle.js:15436:13) at ReactCompositeComponentWrapper.performInitialMount (localhost:8080/public/bundle.js:15708:10) at ReactCompositeComponentWrapper.mountComponent (localhost:8080/public/bundle.js:15619:22) at Object.mountComponent (localhost:8080/public/bundle.js:8009:36) at ReactCompositeComponentWrapper.performInitialMount (localhost:8080/public/bundle.js:15732:35) at ReactCompositeComponentWrapper.mountComponent (localhost:8080/public/bundle.js:15619:22) at Object.mountComponent (localhost:8080/public/bundle.js:8009:36)

我试过使用webpack-dev-server以及...仍然没有喜悦 - 同样的问题。我找不到任何其他提及此错误的信息......可能是什么问题?

import React, { Component } from 'react'; 
import { render } from 'react-dom'; 
import {Router, Route} from 'react-router'; 

class Home extends Component { 
render(){ 
    return (<h1>Hello</h1>); 
} 
} 

render(
<Router> 
    <Route path="/" component={Home}/> 
</Router>, 
document.getElementById('container') 
);' 
+0

您使用的是包的版本是什么? (Check package.json) –

+1

“react”:“^ 15.4.1”, “react-dom”:“^ 15.4.1”, “react-router”:“^ 3.0.0” “babel-core “:”^ 6.18.2“, ”babel-loader“:”^ 6.2.8“, ”babel-preset-es2015“:”^ 6.18.0“, ”babel-preset-react“:”^ “6.16.0”, “webpack”:“^ 1.13.3” – user3244268

+1

不要紧,它是版本3.0.0的一部分,它需要传递一个历史记录选项...我已降级到2.8.1。暂时 – user3244268

回答

0

是的,我测试了它,你需要传递history = {browserHistory}到你的路由器。

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

,不要忘了进口browserHistory:

import { Router, Route, browserHistory, Link, IndexRoute } from 'react-router'; 
+0

嘿!由于“react-router”在其版本更改期间经历了相当大的(突破性的)变化,因此根据OP的“react-router”版本,指定一个有效的“history”属性可能会起作用,无论是否它是'browserHistory','hashHistory'或'memoryHistory'。也许可以避免在你的答案中增加不必要的依赖项,OP没有提及'Link'和'IndexRoute';) – Carsten

相关问题