網(wǎng)站首頁 編程語言 正文
react獲取URL中參數(shù)
這個問題想必很多人都會遇到過,這里我說一下怎么獲取URL中的參數(shù)。
react 獲取URL原理
在 react 組件的 componentDidMount 方法中打印一下 this.props,在瀏覽器控制臺中查看輸出如下:
其中頁面的 url 信息全都包含在 match 字段中,
以地址 ?localhost:3000/app/knowledgeManagement/modify/STY20171011124209535/3/1507701970070/0/?s=1&f=7 ?為例
其中各個參數(shù)定義對應(yīng)如下:
localhost:3000/app/knowledgeManagement/modify/:studyNo/:stepId/:randomNum/:isDefault/?s=&f=
首先打印 this.props.match :
可以看到 this.props.match 中包含的 url 信息還是非常豐富的,其中
-
history
: 包含了組件可以使用的各種路由系統(tǒng)的方法,常用的有 push 和 replace,兩者都是跳轉(zhuǎn)頁面,但是 replace 不會引起頁面的刷新,僅僅是改變 url。 -
location
: 相當(dāng)于URL 的對象形式表示,通過 search 字段可以獲取到 url 中的 query 信息。(這里 state 的含義與 HTML5 history.pushState API 中的 state 對象一樣。每個 URL 都會對應(yīng)一個 state 對象,你可以在對象里存儲數(shù)據(jù),但這個數(shù)據(jù)卻不會出現(xiàn)在 URL 中。實際上,數(shù)據(jù)被存在了 sessionStorage 中)(參考: 深入理解 react-router 路由系統(tǒng)) -
match
: 包含了具體的 url 信息,在 params 字段中可以獲取到各個路由參數(shù)的值。
通過以上分析,獲取 url 中的指定參數(shù)就十分簡單了
下面是幾個例子
// localhost:3000/app/knowledgeManagement/modify/STY20171011124209535/3/1507701970070/0/?s=1&f=7 // localhost:3000/app/knowledgeManagement/modify/:studyNo/:stepId/:randomNum/:isDefault/?s=1&f=7 // 獲取 studyNo this.props.match.match.params.studyNo // STY20171011124209535 // 獲取 stepId this.props.match.match.params.stepId // 3 // 獲取 success const query = this.props.match.location.search // '?s=1&f=7' const arr = query.split('&') // ['?s=', 'f=7'] const successCount = arr[0].substr(3) // '1' const failedCount = arr[1].substr(2) // '7'
注意點:
如果這個值需要在頁面中及時獲得,這個時候就需要注意了,我們都知道react是有生命周期的,那么什么時候獲取URL的值最合適呢?
這個我推薦在componentDidMount 這個生命周期的時候去獲取,因為這個時候頁面已經(jīng)掛在好了,完全可以拿到URL上面的值。?
react獲取頁面跳轉(zhuǎn)URL攜帶的參數(shù)
比如這里有一條攜帶參數(shù)的url。http://baidu.com?type=1
我們要取出type的值。
因為獲取頁面跳轉(zhuǎn)url攜帶的參數(shù)比較常用,所以我們把它封裝成一個工具函數(shù)。
在src根目錄下新建一個文件util.js
封裝一個獲取url參數(shù)的函數(shù)。
export function getUrlParams(name, str) { ? ? const reg = new RegExp(`(^|&)${ name}=([^&]*)(&|$)`); ? ? const r = str.substr(1).match(reg); ? ? if (r != null) return ?decodeURIComponent(r[2]); return null; }
使用的時候,在當(dāng)前頁面導(dǎo)入這個函數(shù)
import { getUrlParams } from '../../util'
接下來就可以在componentDidMount之中獲取這個type值
?componentDidMount(){ ? ? const type = getUrlParams ('type',this.props.location.search); ? ? this.setState({ ? ? ? type?? ?//設(shè)置state ? ? }) ? ? ? }? ?
很簡單,但是記一下
總結(jié)
原文鏈接:https://mayouchen.blog.csdn.net/article/details/78786325
相關(guān)推薦
- 2023-04-12 Docker?login和logout的使用_docker
- 2023-01-26 如何在.Net?7中將Query綁定到數(shù)組詳解_實用技巧
- 2022-11-17 使用flutter的showModalBottomSheet遇到的坑及解決_Android
- 2022-08-31 C++淺析構(gòu)造函數(shù)的特性_C 語言
- 2022-08-17 WPF實現(xiàn)Interaction框架的Behavior擴展_C#教程
- 2023-03-19 Python學(xué)習(xí)之configparser模塊的使用詳解_python
- 2022-08-22 優(yōu)雅使用GoFrame共享變量Context示例詳解_Golang
- 2022-05-03 windows?server?2019開啟iis服務(wù)器+tp5.1的完美配置運行流程_win服務(wù)器
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支