有2条路线的问题 - React Router V6
我在路线上遇到了问题,我有一个auth的路由器,
export const AuthRoute = () => {
return (
<Routes>
<Route path="signIn" element={<SignInPage />} />
<Route path="signUp" element={<SignUpPage />} />
<Route path='/*' element={<Navigate to="/auth/signIn" />} />
</Routes>
)
}
如果用户未经身份验证,则这些是我的仪表板,
export const DashboardRoute = () => {
return (
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="history" element={<HistoryPage />} />
<Route path="contact" element={<ContactPage />} />
<Route path='/*' element={<Navigate to="/" />} />
</Routes>
)
}
那么perticular用户只能访问登录页面,但是如果对用户进行了身份验证,则他会可以访问仪表板中的所有页面。我的主要路由器是,
export const AppRouter = () => {
...
...
if (status === authStatusConst.CHECKING) {
return (
<h3>Loading...</h3>
)
}
return (
<Routes>
{
(status === authStatusConst.UNAUTHENTICATED)
? (
<>
<Route path="/auth/*" element={ <SignInPage /> } />
<Route path="/*" element={ <Navigate to="/auth/signIn" /> } />
</>
)
: (
<>
<Route path="/" element={ <HomePage /> } />
<Route path="/*" element={ <Navigate to="/" /> } />
</>
)
}
</Routes>
)
}
问题是,如果我想访问“历史记录”页面或联系页面,我无法做到,只有我可以访问主页(我已经经过身份验证)。感谢任何建议。
问候,
I got an issue in my routes, I have a router for auth,
export const AuthRoute = () => {
return (
<Routes>
<Route path="signIn" element={<SignInPage />} />
<Route path="signUp" element={<SignUpPage />} />
<Route path='/*' element={<Navigate to="/auth/signIn" />} />
</Routes>
)
}
and these are for my dashboard,
export const DashboardRoute = () => {
return (
<Routes>
<Route path="/" element={<HomePage />} />
<Route path="history" element={<HistoryPage />} />
<Route path="contact" element={<ContactPage />} />
<Route path='/*' element={<Navigate to="/" />} />
</Routes>
)
}
If the user is not authenticated, then that perticular user only has access to the login page, but If the user is authenticated then he will be having access to all the pages in the dashboard. My principal router is,
export const AppRouter = () => {
...
...
if (status === authStatusConst.CHECKING) {
return (
<h3>Loading...</h3>
)
}
return (
<Routes>
{
(status === authStatusConst.UNAUTHENTICATED)
? (
<>
<Route path="/auth/*" element={ <SignInPage /> } />
<Route path="/*" element={ <Navigate to="/auth/signIn" /> } />
</>
)
: (
<>
<Route path="/" element={ <HomePage /> } />
<Route path="/*" element={ <Navigate to="/" /> } />
</>
)
}
</Routes>
)
}
The issue is, If I want an access to the history page or contact page I can't do it, Only I can access the home page (I'm authenticated). I appreciate any suggestion.
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用“/*”时,这意味着您在“/”之后写的任何内容,例如“联系”时,它总是转到您在这种情况下定义的元素,它是主页组件。您可以尝试以下代码。
When you use "/*" that means whatever you write after "/", like "contact", it always goes to that element which you defined in that case, it is the HomePage Component. You can try with the below code.
看来您基本上正在尝试实施路线保护。常见的模式是创建处理身份验证状态检查并重定向或渲染
oflet
的布局路由。不要有条件地渲染路线,而是有条件地允许访问受保护的内容。示例:
...
如果您不要想要在
Applouter
中渲染所有路线和嵌套路线,而是想渲染组件呈现呈现下降路由( ie在descendent路由
组件)中,然后,父路由需要将“*”
通配符匹配器附加到其路径上后代路线也可以匹配。例子:
...
It seems you are basically trying to implement route protection. The common pattern is to create layout routes that handle the authentication status check and redirect or render an
Outlet
accordingly. Don't conditionally render the routes, instead, conditionally allow access to protected content.Example:
...
If you don't want to render all your routes and nested routes in
AppRouter
and instead want to render components rendering descendent routes (i.e. rendered in descendentRoutes
components), then the parent routes need to append the"*"
wildcard matcher to their paths so descendent routes can also be matched.Example:
...