微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据角色在 react router v5 中设置默认登陆页面

如何解决如何根据角色在 react router v5 中设置默认登陆页面

我的路由器设置如下:

function PrivateRoute({ component: Component,isAllowed,...rest }) {
   
   return (
     <Route
       {...rest}
       render={({ location }) =>
         isAllowed ? <Component {...props} /> : (
           <Redirect to={{
             pathname: '/Unauthorized',state: { from: location }
           }} />
         )
       }
     />
   );
 }

return (

<Router history={history}>
<Switch>

    <PrivateRoute component={SearchPage} exact path={["/","/search"]} 
     isAllowed={hasSearchAccess} />

     <PrivateRoute component={SearchResultsPage} exact path="/searchResults" 
     isAllowed={hasSearchAccess} />

      <PrivateRoute component={ApproverPage} exact path="/approve" isAllowed= 
      {hasApproverAccess} />
       </Route>

     </Switch>
   </Router>
 );

现在基于 UserRole(不同的布尔变量)需要显示登录页面。并且该路由应仅在用户登录后运行。刷新时页面不应重定向登录页面。如何实现?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。