簡體   English   中英

如何在不在 react-router-dom 中嵌套 url 的情況下重定向頁面

[英]How to redirect page without nesting the url in react-router-dom

我在我的應用程序中使用 react-router-dom v5 進行路由。 在某些情況下,我有這樣的路線:

checkup/step-1/:id
checkup/step-2/:id
checkup/step-3/:id

例如,我在checkup/step-1/:id所以我想從那里重定向到checkup/step-2/:id 要重定向,我使用hisory.push() 但是結果變成了這樣:

checkup/step-1/:id/checkup/step-2/:id

但我的預期結果是:

checkup/step-2/:id

在開頭使用/
例如:

history.push('/checkup/step-2/:id')

代替

history.push('checkup/step-2/:id')

您可以使用 react-router-dom 提供的Redirect組件

例如:

import {Redirect} from 'react-router-dom'
function MyComponent (){
  return (
    <>
      {condition && <Redirect to='/your-desire-path'/>}
      <MyOtherStaff/>
    </>
  )
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM