javascript - 如何使嵌套的反应路由器组件加载到新页面而不是页面的一半
问题描述
我目前有一种情况,我的文件中有我的主要父路由/开关,App.js
但在文件中的一个路由中App.js
,我有另一个组件,它也有它自己<Router>
的路由/开关设置,它调用另一个名为<MyJobComponent />
我设置的文件层次结构如下:
App.js - has route/path to the component <AllJobs />
|
|---> AllJobs.js - has nested route/path to the component <MyJobComponent />
|
|---> MyJobComponent.js - has the <Link to={`/my-job-id/${item.job_id}`}
在这里,这个组件在AllJobs.js
关卡而不是在App.js
关卡被渲染,这是我希望它被渲染的地方。
<Switch>
<Route exact path="/job-path/:jobID" component={MyJobComponent} />
</Switch>
在<MyJobComponent />
我有以下Link to
代码:
<TableCell style={{width: '10%'}}>
<Link
to={`/my-job-id/${item.job_id}`}
style={{ textDecoration: 'underline', color: 'black' }}
>
{item.job_id}
</Link>
</TableCell>
单击此按钮Link to
会将我的页面呈现在页面的中间<MyJobComponent />
基于 react-router 嵌套示例更新
在此处查看此示例:React-Router Nested如果您单击Topics
然后单击Components
主题下的子链接,您将看到它components
在这些子链接下方显示标题。
这是我想在它自己的页面上显示的子链接下方的这一部分,即components
。这可能吗?
解决方案
将此代码移至 App.js:
<Switch>
<Route exact path="/job-path/:jobID" component={MyJobComponent} />
</Switch>
推荐阅读
- javascript - 难以使用 API 组合参数?
- ios - 如何过滤比较 2 个 JSON 的数据并将结果保存在数组中
- python - Python从列表中每个项目的开头和结尾删除换行符
- xml - 是否存在比 ADODB.Stream 更快的替代方案?
- android - 意图构造之间有什么区别
- c++ - JUCE:滑块,ValueTree 错误
- azure-devops - 在 Azure Devops 中开始开发新模块时应该做些什么?
- react-native - 从 cameraRoll Uri 反应原生渲染图像
- javascript - 将对象序列化为 indexedDB 与 JSON.stringify 的效率
- sql - 错误:不允许组功能