react4.0路由如何跳转

本文讲解"react4.0路由怎么跳转",希望能够解决相关问题。

react4.0路由跳转的方法:1、通过“npm install history --save”安装“history”;2、在要跳转的地方对应的js文件中,引入createHashHistory并执行代码“import { createHashHistory } from 'history'createHashHistory().push('/share')”即可。

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>

关于 "react4.0路由怎么跳转" 就介绍到此。希望多多支持编程宝库

本文讲解"react属于web前端开发框架吗",希望能够解决相关问题。react属于一种web前端开发框架;react是用于构建用户界面的JavaScript库,其特点有:1、简单,简单的表述任意时间点你 ...