使用受保护路由的嵌套路由无法正常工作
早些时候我发布了一个类似的问题: 检查管理的方法在尝试登录时将我重定向到主页
我尝试在 protectedRoute.tsx 内实现受保护的路由:
import { Navigate, Outlet } from "react-router";
import { RootStateOrAny, useSelector } from "react-redux";
interface ProtectRouteProps {}
export default function ProtectRoute(props: ProtectRouteProps) {
const userSignin = useSelector((state: RootStateOrAny) => state.userSignin);
const { userInfo } = userSignin;
return userInfo?.user?.isAdmin ? <Outlet /> : <Navigate to='/' />;
}
我真的不知道 ProtectRouteProps 是什么以及我是什么应该放进去。同样在路由部分,我确实像他告诉我的那样:
<Route path='/createItem' element={<ProtectRoute />} />
<Route path='/createItem' element={<CreateItem />} />
现在的问题是无法访问 CreateItem,因为正在访问的 ProtectRoute 页面是一个空页面。我应该怎么办?
earlier I posted a similar question: Method for checking admin is redirecting me to main page when trying to login
I tried to implement the protected route, inside a protectRoute.tsx:
import { Navigate, Outlet } from "react-router";
import { RootStateOrAny, useSelector } from "react-redux";
interface ProtectRouteProps {}
export default function ProtectRoute(props: ProtectRouteProps) {
const userSignin = useSelector((state: RootStateOrAny) => state.userSignin);
const { userInfo } = userSignin;
return userInfo?.user?.isAdmin ? <Outlet /> : <Navigate to='/' />;
}
I don't really know what ProtectRouteProps is and what I should put in it. Also in the routing part I did like he told me:
<Route path='/createItem' element={<ProtectRoute />} />
<Route path='/createItem' element={<CreateItem />} />
The problem now is that can't access CreateItem because is going on the ProtectRoute page that is an empty one. What should i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有道具。从用法可以清楚地看出这一点:
没有道具传递给
ProtectRoute
。您可以删除 props 对象:“Auth”路由就是所谓的 布局路由 。它们应用一些逻辑,也许是一些样式布局 CSS,并为要渲染的嵌套
Route
组件渲染一个Outlet
。嵌套的Route
组件使用path
属性进行路由匹配。例子:
There are no props. This is clear by the usage:
No props are passed to
ProtectRoute
. You can drop the props object:"Auth" routes are what are called layout routes. They apply some logic, perhaps some styled layout CSS, and render an
Outlet
for nestedRoute
components to be rendered into. The nestedRoute
components use thepath
prop for route matching.Example:
创建受保护的.js
Create Protected.js