javascript - React Router v4 - 嵌套路由不起作用
问题描述
这些是我在 Router.js 中的主要路线
const Routes = () => {
return (
<Switch>
<Route path="/" exact component={HomePage} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
);
}
这些是我在主页下的嵌套路由。现在仪表板和主页正在工作,但忘记密码和注册不工作。我只能看到没有错误的空白页。
render() {
const {match} = this.props;
return (
<div className="container home-grid">
<div className="featured">
<Featured />
</div>
<div className="home-forms">
<div className="logo">
<Logo />
</div>
<TransitionGroup className="route-page">
<CSSTransition
key={location.pathname}
timeout={{ enter: 800, exit: 800 }}
classNames="page"
>
<section className="route-section">
<Switch>
<Route exact path={match.path} component={SignIn}/>
<Route path={`${match.path}forgot-password`} component={ForgotPassword} />
</Switch>
</section>
</CSSTransition>
</TransitionGroup>
<div className="footer-text">
<Text specialClass="footer"></Text>
</div>
</div>
</div>
)
}
登录正在渲染,但其他路由没有。我究竟做错了什么?
解决方案
当您到达路由时发生的情况/forgot-password
是 HomePage 路由不再匹配,因为exact
导致卸载整个 Home 组件,因此它也卸载了子路由。
您必须将子路由上移一级,例如在您定义主路由的 Routes.js 中。或者您可以删除exact
Home 组件并将其作为呈现所有常见元素(例如页眉和页脚)的组件进行威胁,但在这种情况下,我不会将其称为 Home,但可能会Main
这样。
推荐阅读
- android - 如何在一个 android 项目中为多个客户端创建多个 res 文件夹?
- node.js - Node js,Express - 无法使用 post 方法检索表单数据
- typescript - 根据联合类型下多个类型的参数创建Object类型
- typescript - 在ts中,我希望函数的参数是a或b,例如:
- reactjs - 将 npm react-date-picker 限制为仅在反应中动态显示从今天开始的最后 30 天
- reactjs - 如何在反应三纤维中将相机嵌套在一组中
- c# - Selenium C# 代码有效,但需要等待或延迟
- json - 错误:类型“字符串”不是“地图”类型的子类型
' 在类型转换中 - python - MongoDB Python list_database_names() 错误
- yosys - 错误:在库中找不到缓冲门