第一种:this.props.router.push(path)
第二种:this.context.router.push(path)
文件要额外添加一些内容
class Example extends React.Component {
constructor(props, context) {
super(props, context);
this.context.router; // it works
}
...
this.context.router.push(path);
}
Example.contextTypes = {
router: React.PropTypes.object
};
class index extends React.Component {
render() {
return (
<div>
home page
</div>
);
}
componentWillMount() {
if (noLogin == 1) {
this.context.router.push({ pathname : '/', state : { msg : 'you have logined and will redirect to your page'}});
} else if (noLogin == 0) {
this.context.router.push({ pathname : '/login', state : { msg : 'you do not login yet, please login' }});
}
}
}
class LoginPage extends React.Component {
constructor(props, context) {
super(props, context);
}
render() {
return (
<div>
{this.props.location.state}
</div>
);
}
}