You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React.js中通过路由路径控制Navbar显隐异常:登录注册后不显示

解决登录/注册页隐藏Navbar后其他页面无法显示的问题

你的代码逻辑存在明显错误:location.pathname不可能同时等于'/LogIn'和'/patientSignUp',这个判断永远不成立。这就导致——不仅登录、注册页不会显示Navbar,其他所有页面也都满足不了这个条件,自然Navbar也出不来。

把条件改成当路径不是登录/注册页时显示Navbar就可以解决问题,下面是两种可行的写法:

写法一:直接判断不等于目标路径

return (
  <div className="App background-Color">
    {location.pathname !== '/LogIn' && location.pathname !== '/patientSignUp' && <Navbar/>}
    {/* 这里放页面其他内容 */}
  </div>
)

写法二:用数组管理需要隐藏Navbar的路径(更易扩展)

return (
  <div className="App background-Color">
    {!['/LogIn', '/patientSignUp'].includes(location.pathname) && <Navbar/>}
    {/* 这里放页面其他内容 */}
  </div>
)

内容的提问来源于stack exchange,提问作者Foyjur Rahman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 12:54:20