【发布时间】:2021-05-16 09:55:29
【问题描述】:
我正在尝试在我的 React 应用程序中同时使用静态路由和动态路由。我有一个 JSON 文件,其中包含应用程序具有的所有动态路由,如下所示:
[
{
"Text": "Home",
"Link": "/"
},
{
"Text": "Users",
"Link": "/Users"
},
{
"Text": "Posts",
"Link": "/Posts"
}
]
这是我的应用程序(我删除了一些不需要的代码)
import React from 'react';
function App() {
return (
<div className="Grid-Container">
<Router>
<Switch>
<div>
<!-- Static Route to the Settings section -->
<Route
path='/settings/home'
component={Settings}
key={'Settings'}/>
<!-- Dynamic Route which displays all Entries for the chosen Page -->
<Route
path={`/${page.Text}`}
component={objectPage}
key={`${page.Text}-List`}
exact/>
<!-- Dynamic Route which displays one Entry for the chosen Page -->
<Route
path={`/:object/:id`}
component={DetailPage}
key={`Detail-Route`}
exact/>
</div>
</Switch>
</Router>
</div>
);
}
export default App;
动态路线按预期工作,但是当我尝试转到设置页面时,我看到了我不想要的详细信息页面的内容。
【问题讨论】:
标签: reactjs react-router