reactjs - Reactjs重定向路由不起作用但url更新
问题描述
我需要默认从父路径重定向到子路径。但路线正在更新。但该组件未呈现。
这是我的路线详情:
懒惰的部分:
const UK = React.lazy(() => import("./components/page-projects/project-uk/project-uk.component"));
const FR = React.lazy(() => import("./components/page-projects/project-france/project-france.component"));
const LT = React.lazy(() => import("./components/page-projects/project-lithuania/project-lithuania.component"));
路线路径:
<Redirect from="/projects" to="/projects/united-kingdom">
<Route path="/projects/united-kingdom" component={UK}></Route>
<Route path="/projects/france" component={FR}></Route>
<Route path="/projects/lithuania" component={LT}></Route>
</Redirect>
这里有什么问题?有人帮我吗?我的版本react is 17x
解决方案
该<Switch />
组件将仅渲染与路径匹配的第一个路由。一旦找到与路径匹配的第一条路由,它就不会寻找任何其他匹配项。不仅如此,它还允许嵌套路由正常工作,因为这Redirect
是一个你应该包裹在Switch
组件下的路由
<Switch>
<Redirect from="/projects" to="/projects/united-kingdom" />
<Route path="/projects/united-kingdom" component={UK} />
<Route path="/projects/france" component={FR} />
<Route path="/projects/lithuania" component={LT} />
</Switch>
推荐阅读
- regex - Perl 在替换本身中进行替换
- json - 将 JSON 存储到数组中
- python - 从字符串(或 DataFrame 对象)中提取时间数据的更有效方法
- .net - Windows 社区工具包的 WPF IUriToStreamResolver 实现?
- django - 错误未记录到 txt 文件
- python - 在 Python 中逐字索引文本文件
- python - 是否可以在 Python 中按特定顺序获得 3 个特定随机数?
- javascript - 计时器上的自动突出显示图像地图区域(不是悬停)?
- mongodb - Meteor-Files 与谷歌存储块的集成阻止发布用户
- python - 面对这个错误:AttributeError: Can't get attribute 'DeprecationDict' on
运行代码以加载 ML 模型 pickle 文件时遇到此问题。,AttributeError: Can't get attribute 'DeprecationDict' on