reactjs - 如何在 ReactJS 中的模糊路由中停止第一次匹配时的路由器匹配
问题描述
在我的项目中,我有不同页面的路线。我想在第一条路线匹配时停止执行。我的 route.js 文件有这个代码
export default [
{
exact: true,
component: Home,
path: "/"
},
{
exact: true,
component: ShopingCart,
path: "/shopingcart"
},
{
exact: true,
component: LinkChanger,
path: "/:pathname"
}
];
我有一个使用这些 route.js 文件的文件
<Switch>
<Router history={history} >
<div>
{
routes.map( (route, index) =>
<Route key={index} exact={route.exact} path={route.path} component={(props)=><route.component {...props} isAuthed={true} />}/>)
}
</div>
</Router>
</Switch>
当我将链接更改为 /shopingcart 时。首先调用组件 ShopingCart,然后调用 LinkChanger。即使在使用 switch 之后,我的第二个组件 LinkChanger 也会被调用。他们有什么方法可以在第一次比赛时阻止路由器。
解决方案
尝试包裹Switch
在里面Router
-
<Router history={history}>
<Switch>
{
routes.map( (route, index) =>
<Route key={index} exact={route.exact} path={route.path} component={(props)=><route.component {...props} isAuthed={true} />}/>)
}
</Switch>
</Router>
推荐阅读
- android - 我在按下音量按钮时收到此错误
- javascript - 提交表单应在成功提交到 Google 表格后显示消息
- sumo - 如何在多线高速公路上确定密度(车辆/公里)?
- laravel - 如何从 Laravel 和 VueJS 中的两个相关表中选择数据
- flutter - 使同一行中的另一个小部件可见后如何避免小部件位移?
- pdf - 从函数`scatter3d`和RGL输出的R Markdown PDF中的纯图像
- python - 如何分别获取 QuerySet 值?
- java - Retrofit2 POST 请求响应返回 null
- javascript - 如何防止退格键出现额外的“r”字母
- tableau-api - 在甘特图 Tableau 中绘制图标