我的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"
所有重定向到当前应用程序中的页面。
这个问题的解决方案是什么?
发布于 2018-03-22 09:32:44
我是这样实现的:
const AuthenticatedRoute = ({ component: Component, ...rest }) =>
(<Route
{...rest}
render={props =>(
isAuthenticated()
? (<Component {...props} />)
: ( window.location = "http://your_full_url" )
)}
/>);
发布于 2018-12-14 05:12:55
我有一个使用react路由器的create- react -app项目,问题是当输入路径'/ path /*‘时,它会加载,就像在同一个react路由器中一样,即使我已经配置了在express服务器上使用另一个react项目。问题是服务工作者在后台运行,对于'/‘内的任何路由,它都在使用缓存。
我的解决方案是修改'registerServiceWorker.js',这样当你在路径中时,你会忽略服务工作者。我没有深入了解服务工作者,但以下是我使用的代码:
export default function register() {
...
window.addEventListener('load', () => {
const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
if(window.location.href.match(/*\/path\/*/)){
// Reload the page and unregister if in path
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload();
});
});
} else if (isLocalhost) {
...
在路径中时,它取消订阅服务工作线程并重新加载页面。
https://stackoverflow.com/questions/49418692
复制相似问题