特別是react-router-dom v5.2.0?我有一個項目列表,所有這些項目都需要自己的頁面,并且在該動態路由中,我想要一些嵌套路由??梢赃@樣想:myapp.com/:item(主項目頁面)myapp.com/:item/about(關于項目頁面)myapp.com/:item/faq(常見問題解答項目頁面)問題是,按照我實現它的方式,每個嵌套/:item/whatever頁面都會收到 404。這是我的路由當前的樣子:const App = ({}) => {? ? return (? ? ? ? <Router>? ? ? ? ? ? <Switch>? ? ? ? ? ? ? ? <Route exact path='/:id' component={Item}>? ? ? ? ? ? ? ? </Route>? ? ? ? ? ? </Switch>? ? ? ? </Router>? ? )}const Item = ({ match }) => {? ? return (? ? ? ? <div>? ? ? ? ? ? TODO: Item {match.url}? ? ? ? ? ? <Switch>? ? ? ? ? ? ? ? <Route path={`${match.path}/about`}>? ? ? ? ? ? ? ? ? ? <h1>TODO: About</h1>? ? ? ? ? ? ? ? </Route>? ? ? ? ? ? ? ? <Route path={`${match.path}/faq`}>? ? ? ? ? ? ? ? ? ? <h1>TODO: FAQ</h1>? ? ? ? ? ? ? ? </Route>? ? ? ? ? ? </Switch>? ? ? ? </div>? ? );};就目前情況而言,我可以獲得 的頁面/:item,但如果我嘗試轉到它們的嵌套路由,則會收到 404。我的設置有問題嗎?我怎樣才能讓它發揮作用?請注意,我已經嘗試了許多不同的變體:App組件中的嵌套路由有和沒有Switch包裝紙將組件Route作為component嵌套組件的 prop傳遞給編輯:決定包含我的Item組件的一個版本,其中我有我能想到的路線的所有變體/about。
React router v5:需要動態路由中的嵌套路由
蕪湖不蕪
2023-08-10 15:22:15