react4.0路由跳转的方法:1、通过“npm install history --save”安装“history”;2、在要跳转的地方对应的js文件中,引入createhashhistory并执行代码“import { createhashhistory } from 'history'createhashhistory().push('/share')”即可。
本教程操作环境:windows10系统、react-router v4.0版、dell g3电脑。
react 4.0 路由怎么跳转?
react-router v4.0 hashrouter使用js跳转
react-router v4.0上已经不推荐使用hashrouter,主推browserrouter,但是因为使用browserrouter需要服务端配合可能造成不便,有时还是需要用到hashrouter。
下面是v4.0的react-router中hashrouter以js方式跳转的实现步骤。
v4.0剥离了history,所以要操作history,需要安装支持包:
npm install history --save
在要跳转的地方对应的js文件中,引入createhashhistory并执行代码,以跳转到'/share'举例:
import { createhashhistory } from 'history'createhashhistory().push('/share')
已经ok了。
在使用上述方法跳转之前,需要确认已经定义router,可参考下述代码:
import { hashrouter as router, route, switch } from 'react-router-dom'...<router> <app> <switch> <route path='/index' component={显示的组件1}> <route path='/share' component={显示的组件2}> ... </switch> </app></router>
推荐学习:《react视频教程》
以上就是react 4.0 路由怎么跳转的详细内容。