我必须重定向到/user/dashboard
登录后的页面,但每次我关闭选项卡并再次打开它时,它都会打开默认主页,即/
.
import React from 'react';
import {isAuthenticated} from '../helpers/auth'
import {Route, Redirect } from 'react-router-dom';
const UserRoute = ({component: Component, ...rest}) => {
return(
<Route
{...rest}
render={(props)=>
isAuthenticated() && isAuthenticated().role === 0 ? (
<Component {...props} />
):(
<Redirect to="/signin" />
)
}
/>
);
}
导出默认用户路由;
这是路由设置的一般示例,它检查“公共”、“私有”和索引路径:
export default function Routes() {
function fromIndex() {
if (isAuthenticated) { // use your authentication mechanism
return '/user/dashboard'
}
return '/login'
}
return (
<Router history={history}> {/* history not required when using BrowserRouter */}
<Switch>
<Redirect exact from="/" to={fromIndex()} />
{/* publicPaths is an array of paths: [{...} */}
{publicPaths.map(item => (
<PublicRoute exact={item.exact} key={item.path} path={item.path}>
<item.component />
</PublicRoute>
))}
{privatePaths.map(item => (
<PrivateRoute exact={item.exact} key={item.path} path={item.path}>
<item.component />
</PrivateRoute>
))}
<NotFoundPage />
</Switch>
</Router>
)
}
Here, PrivateRoute
是一个渲染的组件children
如果通过身份验证,否则重定向回“/login”。
And PublicRoute
是一个渲染的组件children
如果未通过身份验证,则重定向回“/”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)