react中路由传参和url传参

一、如果路由跳转的页面是必须要一个参数的,那么可以在路由配置文件中的path中添加要传递参数的参数名。

path: 'account-info/:id'

接受参数的时候使用react-router-dom中的useParams函数就可以了。

const { id } = useParams()

二、如果需要跳转的页面可以有参数也可以没有,使用这个方法就会造成歧义,使用useParams取值会取成:id。

可以换成下面这种方法

navigate(`/merchant?store_id=${data.data.store.store_id}`)   //存值
const location = useLocation()
const searchParams = new URLSearchParams(location.search)
const store_id = searchParams.get('store_id')     //取值

 

posted @ 2024-04-07 16:33  数星观月  阅读(29)  评论(0编辑  收藏  举报