0

所以它处理路由器导航<Link />的onClick的实现很简单:阵营路由器定义我自己的链接

const { history } = this.context.router; 

history.push(to); 

即从github repo

因此,在我application它应该工作拉,因为上下文从<Router>组件传递下来了吗?

class App extends React.Component { 
    constructor() { 
    super(...arguments); 
    } 

    render() { 
    const home =() => (<div>Home</div>); 
    const about =() => (<div>About</div>); 
    const { history } = this.context.router; 

    console.log("History", history); 

    return (
     <div> 
     <ul> 
      <li><Link to="/">Home</Link></li> 
      <li><Link to="/about">About</Link></li> 
     </ul> 

     <Route exact path="/" component={home} /> 
     <Route path="/about" component={about} /> 
     </div> 
    ) 
    } 
} 

ReactDOM.render(
    <Router> 
    <App /> 
    </Router>, 
    document.getElementById('main') 
); 

但你可以在控制台输出中看到,历史的对象是不确定....

+0

with'withRouter' – azium

回答

0

只是为了澄清Azium评论:

class App extends React.Component { 
    constructor() { 
    super(...arguments); 
    } 

    render() { 
    const home =() => (<div>Home</div>); 
    const about =() => (<div>About</div>); 
    const { history } = this.props; 

    console.log("History", history); 

    return (
     <div> 
     <ul> 
      <li><Link to="/">Home</Link></li> 
      <li><Link to="/about">About</Link></li> 
     </ul> 

     <Route exact path="/" component={home} /> 
     <Route path="/about" component={about} /> 
     </div> 
    ) 
    } 
} 

const AppWithRouter = withRouter(App); 

ReactDOM.render(
    <Router> 
    <AppWithRouter /> 
    </Router>, 
    document.getElementById('main') 
); 

withRouter给你匹配,位置,历史上的道具。

+0

是否有效?不适合我我有很多错误 –

+0

是的,工作正常。你在尝试什么? jsbin? –

+0

jsbin有很多与库导入相关的警告和错误 –