2016-11-29 21 views
48

Tôi đang sử dụng phiên bản mới nhất của react-router (phiên bản^3.0.0).React-Router - Uncaught TypeError: Không thể đọc thuộc tính 'getCurrentLocation' của undefined

tôi đã viết như sau định tuyến sử dụng ES5:

routes.js:

var React = require("react"); 
var Router = require("react-router"); 
var AuthorPage = require('./components/authors/authorPage') 
var App = require('./components/app') 
var Route = Router.Route; 

var routes = (
    <Route path="/" component={App}> 
     <Route path="authors" component={AuthorPage}/> 
    </Route> 
); 

module.exports = routes; 

Trong một tập tin JS gọi main.js tôi thực hiện cuộc gọi như sau:

main.js:

var React= require("react"); 
var ReactDom = require("react-dom"); 
var Router = require('react-router').Router; 
var routes = require('./routes'); 
ReactDom.render(<Router routes={routes}></Router>, document.getElementById('app')); 

Khi tôi chạy đồng Tôi có ngoại lệ sau trong các công cụ dành cho nhà phát triển Google Chrome:

Uncaught TypeError: Cannot read property 'getCurrentLocation' of undefined

Tại sao lại như vậy? Tôi đang thiếu gì?

Trả lời

8

Đã cùng một lỗi, giải quyết nó theo cách này:

Thêm

var hashHistory = ReactRouter.hashHistory; 

Và trong Router thiết lập lịch sử tương đương với hashHistory

<Router history={hashHistory}> 

Vì vậy, trong trường hợp của bạn:

var routes = (
<Router history={hashHistory}> 
    <Route path="/" component={App}> 
     <Route path="authors" component={AuthorPage}/> 
    </Route> 
</Router> 
); 
Các vấn đề liên quan