[英]React router dom v6 redirect to home when directly enter the URL
我正在嘗試為我的 React 網站建立私人和公共路線。
我有一個問題:當我嘗試直接訪問帶有路徑名的鏈接時,它會重定向到主頁。
例如,當直接輸入鏈接時,我有一個帶有路徑名稱dashboard
的私有路由: https://localhost:3000/dashboard
仍然會重定向到https://localhost:3000/
。 我猜這是因為 PrivateRoutes() 函數,它總是將路徑重定向到主頁但不確定如何修改它。
應用程序.js
const App = () => {
const {
state: { isAuthenticated },
} = AuthContext();
function PrivateRoutes() {
return isAuthenticated ? <Outlet /> : <Navigate to="/login" />;
}
function PublicRoutes() {
return isAuthenticated ? <Navigate to="/home" /> : <Outlet />;
}
return (
<BrowserRouter>
<Routes>
<Route element={<PrivateRoutes />}>
<Route path="/*" element={<Layout />} />
</Route>
<Route path="*" element={<Error />} />
<Route path="login" element={<PublicRoutes />}>
<Route path="/login" element={<Login />} />
</Route>
</Routes>
</BrowserRouter>
);
};
布局.js
const Layout = () => {
return (
<Box>
<Header />
<Routes>
<Route index element={<Home />} />
<Route path="dashboard" element={<Dashboard />} />
<Route path="transaction" element={<Transaction />} />
<Route path='/*' element={<Navigate to="/" />} />
</Routes>
<Footer />
</Box>
);
};
<Routes>
<Route path="/" element={<Home/>} />
<Route path="/login" element={<Login/>} />
<Route path="/dashboard"element={
token? <Admin/> :<Login/>
} />
</Routes>
我是這樣做的嘗試它而不是在額外的功能中獲得它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.