首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用React路由器V4重定向至ReactJS组件上的服务器路由

使用React路由器V4重定向至ReactJS组件上的服务器路由
EN

Stack Overflow用户
提问于 2018-03-22 08:33:03
回答 2查看 1.5K关注 0票数 2

我的React路由器V4路由是这样构造的:

代码语言:javascript
复制
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

我尝试过但没有成功的东西:

代码语言:javascript
复制
<Redirect to="//registration' />
windows.location = "/registration"
windows.location.href = "/registration"
<Redirect to="http//registration' />
windows.location = "http://registration"
windows.location.href = "http://registration"

所有重定向到当前应用程序中的页面。

这个问题的解决方案是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-22 09:32:44

我是这样实现的:

代码语言:javascript
复制
const AuthenticatedRoute = ({ component: Component, ...rest }) =>
    (<Route
        {...rest}
        render={props =>(
            isAuthenticated()
                ? (<Component {...props} />)
                : ( window.location = "http://your_full_url" )
        )}
    />);
票数 1
EN

Stack Overflow用户

发布于 2018-12-14 05:12:55

我有一个使用react路由器的create- react -app项目,问题是当输入路径'/ path /*‘时,它会加载,就像在同一个react路由器中一样,即使我已经配置了在express服务器上使用另一个react项目。问题是服务工作者在后台运行,对于'/‘内的任何路由,它都在使用缓存。

我的解决方案是修改'registerServiceWorker.js',这样当你在路径中时,你会忽略服务工作者。我没有深入了解服务工作者,但以下是我使用的代码:

代码语言:javascript
复制
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) {
    ...

在路径中时,它取消订阅服务工作线程并重新加载页面。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49418692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档