2016-11-29 53 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

相關問題