通过Link标签跳转
Link标签相当于原生js中的a标签,用法基本类似。to属性相当于a标签中的href。
<Link to="/login">点击跳转到登录页面</Link>
通过this.props.history.pushState跳转
this.props.history.pushState({passParam: true}, "/targetURL");
第一个参数为跳转页面时,需传递的参数,若不需要传参数,则为null。
第二个参数为跳转的目标页面。
使用browserHistory.push
const path = `/login/${userName}`
browserHistory.push(path)
使用context对象。
使用不同的方法创建对象时,使用context的方法也不尽相同。
export default React.createClass({
contextTypes: {
router: React.PropTypes.object
},
handleSubmit(event) {
// ...
this.context.router.push(path)
},
})
class Demo extends Component{
static contextTypes = {
router: React.PropTypes.object.isRequired
};
go(id) {
const {router}= this.context;
router.push(`/project?id=${id}`);
}
}