reactjs - React-Router:始终显示标题但不在登录页面中
问题描述
我当前的路由器树如下所示:
<Route exact path="/" component={Landing} />
<Route component={Header} />§
<Route path="/component1" component={Component1} />
<Route path="/component2" component={Component2} />
...
我希望<Header />
在渲染组件时始终显示<Component1 />
,<Component2 />
而不是在用户位于登录页面 ( <Landing />
) 时显示。
但是,对于上面的反应路由器树,<Header />
总是显示。
解决方案
您可以使用Switch
fromreact-router-dom
并配置您的路线,例如
<Switch>
<Route exact path="/" component={Landing} />
<Route component={Header} />
</Switch>
<Route path="/component1" component={Component1} />
<Route path="/component2" component={Component2} />
在上述情况下,如果Landing
路由匹配,Header
则不渲染,否则Header
渲染
推荐阅读
- android - 如何在kotlin中正确地为字符串赋值
- c++ - 基类未捕获 C++ 异常
- java - 有条件地分配变量以匹配 xslt 模板中的路径
- angular - Angular - 反应形式选择输入框 - 下拉选项问题
- javascript - 如何设置 eslint 以仅在单个文件 vue 组件中将 lodash 检测为全局?
- javascript - 如何在视频标签中隐藏 iPhone 中的全屏原生按钮?
- angular - 在 Angular 中使用指令 + 模板实现 AngularJS 的最简单方法是什么?
- c++ - Linux 上的 SAFEARRAY
- ocaml - 如何在 OCAML 中使用 TSDL 检查按下的键?
- selenium - Chrome 没有最大化