我的React路由器V4路由是这样构造的:
const isAuthenticated = () => {
let hasToken = localStorage.getItem("jwtToken");
if (hasToken) return true;
return false;
};
const AuthenticatedRoute = ({ component: Component, ...rest }) =>
<Route
{...rest}
render={props =>
isAuthenticated()
? <Component {...props} />
: <I NEED TO REDIRECT FROM HERE TO SERVER PAGE />}
/>;
class App extends Component {
render() {
return (
<BrowserRouter basename="/editor">
<Switch>
<AuthenticatedRoute exact path="/" component={AppNav} />
<AuthenticatedRoute
exact
path="/:module"
component={AppNav}
/>
<AuthenticatedRoute
exact
path="/:module/:screen"
component={AppNav}
/>
<AuthenticatedRoute
exact
path="/:module/:screen/:action"
component={AppNav}
/>
<AuthenticatedRoute
exact
path="/:module/:screen/:action/:id"
component={AppNav}
/>
<Route component={PageNotFoundError} />
</Switch>
</BrowserRouter>
);
}
}
export default App;
正如你在代码中看到的,如果没有通过身份验证,我想要重定向到服务器页面。该页面是另一个用于管理用户注册的react应用程序,它位于服务器中,但位于另一个路由树中:/registration
我尝试过但没有成功的东西:
<Redirect to="//registration' />
windows.location = "/registration"
windows.location.href = "/registration"
<Redirect to="http//registration' />
windows.location = "http://registration"
windows.location.href = "http://registration"
所有重定向到当前应用程序中的页面。
这个问题的解决方案是什么?
https://stackoverflow.com/questions/49418692
复制相似问题