react-antd中使用umi的Link路由組件跳轉頁面時如何傳參?

在使用Link組件跳轉頁面時有兩種頁面跳轉,分別爲:
1、在當前標籤頁頁面跳轉
2、跳轉到新的標籤頁

一、在當前標籤頁中路由跳轉

代碼如下:

const { voucherType,organizationId } = record;
<Link to={{pathname:'/voucher/voucherSummary/voucherSummaryDetail', query:{voucherType,organizationId}}}>查看</Link>

如何獲取query中的傳值,在這裏需要用到location,location下面會帶有query對象,裏面就是傳過來的值:

const {
  location:{
    query:{ voucherType,organizationId }
  }
} = this.props;

第一種形式直接使用Link組件的to屬性,pathname的值就是在router.config.js中定義的要跳轉頁面的路由,比如我的router.config.js中定義的路由爲:

{
  path: '/voucher/voucherSummary/voucherSummaryDetail', //調用此處的路徑,就會自動調用component屬性下的組件中
  name: 'voucherSummaryDetail',
  hideInMenu: true,
  component: './Voucher/VoucherSummary/VoucherSummaryDetail', //組件
},
二、跳轉到新的標籤頁

與第一種不同的是,這時我們傳參不用query屬性,而是需要使用url地址傳參,就像get的請求方式,因爲這樣才能將參數帶到新的標籤頁中:

const { voucherType, organizationId, actionId } = record;
<Link to={`/voucher/voucherSummary/voucherSummaryDetail?voucherType=${voucherType}&organizationId=${organizationId}&actionId=${actionId} `} target='_blank'>查看</Link>

取值方式與第一種傳參取值方式相同:

const {
  location:{
    query:{ voucherType,organizationId }
  }
} = this.props;

跳轉之前的url(路徑末尾的 /list 爲重定向時定義的值,請忽略不計):
在這裏插入圖片描述

跳轉到新頁面的url:
在這裏插入圖片描述在項目中常用的路由傳值就是以上兩種方式了,我相信路由傳值方式不止以上兩種,若大家使用的是其他的傳值方式可以留言,我也去研究研究,共勉。

發佈了44 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章