如何根据角色在 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 (将#修改为@)