網站首頁 編程語言 正文
路由組件和組件的區別
路由組件時被Router組件使用的組件,this.props里面有三個參數,分別是history、match、location
可以接收到路由跳轉傳參,也可以進行編程式導航跳轉
普通組件只有父傳子的props值
Swith內置組件使用
作用:當匹配一個路由組件時,其他組件不會被使用,可以加入404頁面,給用戶進行友好提示,提升用戶體驗
react 路由傳參
方式一:url的query方式傳參,在App組件中
//傳值 <Link to='/home?name=張三&age=18'>主頁面</Link> //接收 <Route path='/home' component={home}></Route>
如果我們要打印我們接收到的值,有兩種方式
第一種,在home組件中,創建一個生命周期,然后進行普通的切割、添加、打印即可
componentDidMount(){
console.log(this.props.history.location.search);
let a=this.props.history.location.search.slice(1)
let b=a.split('&')
console.log(b);
let obj={}
b.forEach((item)=>{
item.split('=')
obj[item.split('=')[0]]=item.split('=')[1]
})
console.log(obj);
}
第二種:使用querystring,在使用之前,需要下載引入
下載:npm i querystring -D
componentDidMount(){
let a=this.props.history.location.search.slice(1)
console.log(querystring.parse(a));
}
在頁面使用:querystring.parse(url形式攜帶的字符串)
方式二:url的params傳參
//傳值 <Link to='/login/zhangsan/18'>登錄</Link> //接收 <Route path='/login/:name/:age' component={login}></Route>
注意:傳入的參數跟值得長度必須一致,否則會報錯
打?。?/p>
componentDidMount(){
// console.log(this.props);
console.log(this.props.match.params);
}
方式三:
//傳值 <Link to={{pathname:'/zhuce',user:{name:'張三',age:19}}}>注冊頁面</Link> //接收 <Route path='/zhuce' component={zhuce}></Route>
打?。?/p>
componentDidMount(){
console.log(this.props.location.user);
}
編程式導航
我們定義一個按鈕,在按鈕中給他一個點擊事件,在事件函數中我們進行路由得跳轉
home組件
export default class home extends Component {
onchange=()=>{
this.props.history.push('/home/?name=張三&age=19')
}
render() {
return (
<div>
<button onClick={()=>{this.onchange()}}>點擊跳轉到home頁</button>
</div>
)
}
}
在home 組件中,this.props.history.push后面跟上邊三種傳參方式
app組件
<Link to='/home?name=張三&age=18'>主頁面</Link>
Redirect重定向
跟我們vue中的redirect使用方法相似,用來路由跳轉
<Redirect to='/'></Redirect>
原文鏈接:https://blog.csdn.net/qq_60976312/article/details/125555745
相關推薦
- 2022-03-06 C#中List用法介紹詳解_C#教程
- 2023-06-04 Pandas.DataFrame時間序列數據處理的實現_python
- 2022-11-13 Python?argparse模塊實現解析命令行參數方法詳解_python
- 2022-09-16 Android?運用@JvmName解決函數簽名沖突問題詳解_Android
- 2023-02-12 Jupyter?Notebook運行代碼無反應問題及解決方法_python
- 2022-07-29 C++超詳細講解函數對象_C 語言
- 2022-10-21 一文詳解?Compose?Navigation?的實現原理_Android
- 2023-06-17 關于生產消費者模型中task_done()的具體作用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支