zl程序教程

您现在的位置是:首页 >  工具

当前栏目

react路由跳转

路由React 跳转
2023-06-13 09:11:46 时间

大家好,又见面了,我是你们的朋友全栈君。

React中几种页面跳转方式

一般适用于,点击按钮或其他组件进行页面跳转,具体使用方式如下:

<Link
    to={ 
   { 
   
        pathname: '/path/newpath',
        state: { 
     // 页面跳转要传递的数据,如下
              data1: { 
   },
              data2: []
        },
    }}
>
   <Button>
        点击跳转
   </Button>
</Link>

2、使用 react-router-redux 中的 push 进行页面跳转

react-router-redux 中包含以下几个函数,一般会结合redux使用:

push – 跳转到指定路径 replace – 替换历史记录中的当前位置 go – 在历史记录中向后或向前移动相对数量的位置 goForward – 向前移动一个位置。相当于go(1) goBack – 向后移动一个位置。相当于go(-1) 具体使用时通过发送disppatch来进行页面跳转:

let param1 = { 
   }
dispatch(push("/path/newpath'", param1));
dispatch(replace("/path/newpath'", param1));

3、使用RouteComponentProps 中的history进行页面回退

一般在完成某种操作,需要返回上一个页面时使用。

this.props.history.goBack();  

4、打开一个新的tab页,并截取路径

首先定义路由为 :

path: "/pathname/:param1/:param2/:param3",

点击事件跳转到新页面 打开一个新的tab:

window.open(pathname/{param1}/{param2}/

param1:  this.props.match.params.param1, 
param2:  this.props.match.params.param2, 
param3:  this.props.match.params.param3, 

获取路径参数 :

path?key1=value1&key2=value2
const query = this.props.match.location.search 
const arr = query.split('&')  // ['?key1=value1', '&key2=value2']
const successCount = arr[0].substr(6) // 'value1'
const failedCount = arr[1].substr(6) // 'value2'

或者

function GetUrlParam(url, paramName) { 
   
  var arr = url.split("?");

  if (arr.length > 1) { 
   
    var paramArr= arr[1].split("&");
    var arr;
    for (var i = 0; i < paramArr.length; i++) { 
   
      arr = paramArr[i].split("=");

      if (arr != null && arr[0] == paramName) { 
   
        return arr[1];
      }
    }
    return "";
  }else { 
   
    return "";
  } 
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140807.html原文链接:https://javaforall.cn