You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportconstCheckLogin=({ children }: any)=>{constnavigate=useNavigate()constparams=useParams()const[userInfo,setUserInfo]=useState("")constgetUserInfo=()=>{constuserInfo=localStorage.getItem("cms-token")if(userInfo){setUserInfo(userInfo)}else{message.info("未登录,正在为您跳转到登录页...",1)setTimeout(()=>{navigate("/login")},1500)}}useEffect(()=>{getUserInfo()},[userInfo])if(!userInfo){returnnull}returnchildren}//路由重定向,如果是/,则重定向到列表页exportconstNavigateToList=({ children }: any)=>{constnavigate=useNavigate()constlocation=useLocation()useEffect(()=>{if(location.pathname==="/"){navigate("/listtable")}},[location.pathname])returnchildren}
react-router-middleware-plus
react-router-middleware-plus
是基于react-router v6的路由权限配置开源库,引入中间件middleware
的概念,零成本式路由权限解决方案。midleware
定义为中间件的概念,是包含了一个或多个用户自定义的Auth Component
鉴权组件数组,react-router-middle-plus
会在内部维护一个嵌套高阶组件,在页面路由加载时,会依次执行中间件中的Auth Component
。如果想拦截路由在Auth Component
中直接返回null
即可,如果允许通过返回children
即可。路由配置
在需要路由鉴权的组件添加middleware字段,从左到右依次执行鉴权函数。
鉴权函数
渲染
config.tsx
Component 渲染
Hook渲染
index.tsx
参考文章:
The text was updated successfully, but these errors were encountered: