zoukankan      html  css  js  c++  java
  • React-Router v4.0 hashRouter使用js跳转

    React-Router v4.0上已经不推荐使用hashRouter,主推browserRouter,但是因为使用browserRouter需要服务端配合可能造成不便,有时还是需要用到hashRouter。下面是v4.0的React-Router中hashRouter以js方式跳转的实现步骤。

    1. v4.0剥离了history,所以要操作history,需要安装支持包:
    npm install history --save
    
    1. 在要跳转的地方对应的js文件中,引入createHashHistory并执行代码,以跳转到'/share'举例:
    import { createHashHistory } from 'history'
    createHashHistory().push('/share')
    
    1. 已经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>



  • 相关阅读:
    日志记录
    Ajax
    servlet3.0新特性
    文件上传和下载
    过滤器
    listener
    JavaWeb案例:登陆和注册
    jsp
    cookie和session
    HttpRequest,HttpResponse,乱码,转发和重定向
  • 原文地址:https://www.cnblogs.com/lan-cheng/p/10541626.html
Copyright © 2011-2022 走看看