1
我試圖導航到一個路由到另一個,我設置我的路由方式方面:反應,反應路由器不確定
App.js:
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, hashHistory } from 'react-router';
// Pages
import Layout from './pages/Layout/Layout';
import Home from './pages/Home/Home';
import CityOverview from './pages/CityOverview/CityOverview';
import Game from './pages/Game/Game';
ReactDOM.render(
<Router history={hashHistory}>
<Route path='/' component={Layout}>
<IndexRoute component={Home}></IndexRoute>
<Route path='/city-overview' component={CityOverview}></Route>
<Route path='/game' component={Game}></Route>
</Route>
</Router>,
document.getElementById('app')
);
我再嘗試做導航在這個類:
import React from 'react';
class MyComponent extends React.Component {
constructor() {
super();
this.state = {
email : '',
password : ''
};
}
render() {
// ...
}
navigate() {
this.context.router.push('/city-overview');
}
}
MyComponent.contextTypes = {
router: React.PropTypes.object.isRequired
};
export default MyComponent;
然而,當這個運行時,我得到一個「上下文是未定義的錯誤」 ......