最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
详解各版本React路由的跳转的方法
时间:2022-06-25 16:05:37 编辑:袖梨 来源:一聚教程网
前言
React-Router已经发布了多个版本,利用路由导航的使用方法都不大一样,在这里总结一下。
React-Router 2.0.0 版本
2.0.0版本需要使用browserHistory进行跳转,具体参考一下代码:
import { browserHistory } from 'react-router'
browserHistory.push('/path')
React-Router 2.4.0版本以上
React-Router 2.4.0版本以上,可以通过mixin的方法,使组件增加this.router属性,然后进行相应的操作,具体mixin代码参考如下:
import { withRouter } from 'react-router';
clsss ABC extends Component {
}
module.exports = withRouter(ABC);
用过mixin的组件,会具有this.router的属性,只需要使用this.props.router.push('/path') 就可以跳转到相应的路由了。
React-Router 3.0.0版本以上
3.0.0版本以后不需要再手动mixin相关的router属性,在任何需要跳转的组件中写this.props.router.push('/path') 就可以跳转到响应的路由了。
React-Router 4.0版本以上
路由的跳转
React-Router 4.0对路由进行了改进,router属性改为了history属性,使用方法还是和3.0差不多,任何需要跳转的地方使用this.props.history.push('/path') 就可以进行跳转了
参数的获取
使用this.props.match.params.xxx 可以获取到当前路由的参数
相关文章
- Ubuntu怎么添加Programmer Dvorak键盘? 09-12
- Ubuntu怎么安装轻量级的LXDE桌面? 09-12
- Kimi K3 的公开编码基准成绩有哪些? 09-12
- Kimi K3 在真实工程任务中能否胜任系统设计和代码实现? 09-12
- Ubuntu系统怎么设置文件默认打开方式? 09-12
- Claude Fable 5 是否会永久改为 usage credits 计费? 09-12