【问题标题】:How to disable particular route when other one is active in react router当其他路由在反应路由器中处于活动状态时如何禁用特定路由
【发布时间】:2021-02-22 07:19:48
【问题描述】:

我有运动和娱乐两个部分。我希望当我的运动路线处于活动状态时,用户不应访问娱乐路线。我该怎么做??

我的 App.js 文件中的路由代码

<Router>
  <Switch>
    <Route path="/" exact>
      <Redirect to="/home" />
    </Route>
    <Route path="/home" component={Main} />
    <Route path="/sports" component={Sports} />
    <Route path="/entertainment" component={Entertainment} />
   
  </Switch>
</Router>

【问题讨论】:

标签: reactjs react-router react-router-dom react-router-v4


【解决方案1】:

你可以分成两部分。

举例:

const Routers = () => {
  const toggleRoutesPermission = true // should get the permission value from somewhere

  return (
    <Router>
      <Switch>
        <Route path="/" exact>
          <Redirect to="/home" />
        </Route>
        <Route path="/home" component={Main} />
        {
          toggleRoutesPermission
            ? <Route path="/sports" component={Sports} />
            : <Route path="/entertainment" component={Entertainment} />
        }
      </Switch>
    </Router>
  )
}

【讨论】:

    猜你喜欢
    • 2016-12-28
    • 2018-03-24
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多