【问题标题】:Redirect to server route on ReactJS component using React Router V4使用 React Router V4 重定向到 ReactJS 组件上的服务器路由
【发布时间】:2018-08-31 07:15:37
【问题描述】:

我的 React Router 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;

正如您在代码中看到的,如果未通过身份验证,我想重定向到服务器页面。该页面是另一个管理用户注册的反应应用程序,位于服务器中,但在另一个路由树中:/registration

我尝试过的没有成功:

<Redirect to="//registration' />
windows.location = "/registration"
windows.location.href = "/registration"
<Redirect to="http//registration' />
windows.location = "http://registration"
windows.location.href = "http://registration"

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

解决办法是什么?

【问题讨论】:

  • 尽量提供完整的网址,例如window.location="https://your_website.com/your/server/path"
  • 试过了。不工作。
  • 您有任何错误吗?并确保它是window 而不是windows
  • 感谢指正。确实是window。没有错误。移动到localhost:/editor/registrationlocalhost:/editor/https://registration。不会离开 App 的 React 路由器。
  • 首先尝试&lt;Redirect to="/registration" /&gt;。也许首先检查令牌并在渲染之后放置它更容易:if (!hasToken) return &lt;Redirect to='/registration' /&gt;; 之后放置

标签: reactjs routes react-router-v4


【解决方案1】:

我有一个带有 react 路由器的 create-react-app 项目,问题是当输入路径“/path/*”时,它就像在同一个 react 路由器中一样加载,即使我有使用另一个 react 的配置带有快速服务器的项目。 问题是服务人员在后台运行,并且对于“/”内的任何路由,它都在使用缓存。
我的解决方案是修改“registerServiceWorker.js”,这样当你在路径内时,你会忽略服务工作者。我没有深入了解 Service Worker,但这里是我使用的代码:

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】:

我是这样实现的:

const AuthenticatedRoute = ({ component: Component, ...rest }) =>
    (<Route
        {...rest}
        render={props =>(
            isAuthenticated()
                ? (<Component {...props} />)
                : ( window.location = "http://your_full_url" )
        )}
    />);

【讨论】:

  • 适用于外部链接(例如:http://www.stackoverflow.com,但不重定向到自己的服务器。重定向到我自己的服务器时出现以下错误 - window.location = "/login"You are attempting to use a basename on a page whose URL path does not begin with a basename. Expected path "/login" to begin with "/editor"。对于确定问题与&lt;BrowserRouter basename="/editor"&gt;...
  • 我会接受这个作为答案并打开一个与 basename 相关的新问题。
  • 如果您发现任何基于 React-router 的解决方案,请与我们分享
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2018-10-14
  • 2017-04-09
  • 2019-07-01
  • 1970-01-01
相关资源
最近更新 更多