網(wǎng)站首頁 編程語言 正文
路由組件和組件的區(qū)別
路由組件時(shí)被Router組件使用的組件,this.props里面有三個(gè)參數(shù),分別是history、match、location
可以接收到路由跳轉(zhuǎn)傳參,也可以進(jìn)行編程式導(dǎo)航跳轉(zhuǎn)
普通組件只有父傳子的props值
Swith內(nèi)置組件使用
作用:當(dāng)匹配一個(gè)路由組件時(shí),其他組件不會(huì)被使用,可以加入404頁面,給用戶進(jìn)行友好提示,提升用戶體驗(yàn)
react 路由傳參
方式一:url的query方式傳參,在App組件中
//傳值 <Link to='/home?name=張三&age=18'>主頁面</Link> //接收 <Route path='/home' component={home}></Route>
如果我們要打印我們接收到的值,有兩種方式
第一種,在home組件中,創(chuàng)建一個(gè)生命周期,然后進(jìn)行普通的切割、添加、打印即可
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>
注意:傳入的參數(shù)跟值得長度必須一致,否則會(huì)報(bào)錯(cuò)
打印:
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>
打印:
componentDidMount(){
console.log(this.props.location.user);
}
編程式導(dǎo)航
我們定義一個(gè)按鈕,在按鈕中給他一個(gè)點(diǎn)擊事件,在事件函數(shù)中我們進(jìn)行路由得跳轉(zhuǎn)
home組件
export default class home extends Component {
onchange=()=>{
this.props.history.push('/home/?name=張三&age=19')
}
render() {
return (
<div>
<button onClick={()=>{this.onchange()}}>點(diǎn)擊跳轉(zhuǎn)到home頁</button>
</div>
)
}
}
在home 組件中,this.props.history.push后面跟上邊三種傳參方式
app組件
<Link to='/home?name=張三&age=18'>主頁面</Link>
Redirect重定向
跟我們vue中的redirect使用方法相似,用來路由跳轉(zhuǎn)
<Redirect to='/'></Redirect>
原文鏈接:https://blog.csdn.net/qq_60976312/article/details/125555745
相關(guān)推薦
- 2022-09-15 Go語言操作redis數(shù)據(jù)庫的方法_Golang
- 2022-10-01 react使用useState修改對象或者數(shù)組的值無法改變視圖的問題_React
- 2022-08-13 圖像處理之matlab中meshgrid函數(shù)用法詳解
- 2022-04-29 Go語言中的并發(fā)goroutine底層原理_Golang
- 2023-01-09 Spark處理trick總結(jié)分析_相關(guān)技巧
- 2022-03-29 python?Tkinter是什么_python
- 2023-02-12 Golang反射模塊reflect使用方式示例詳解_Golang
- 2022-12-15 Pycharm中配置Anaconda解釋器的完整步驟_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支