简体   繁体   English

在React Router V4中,如何导航到定义的基本名称之外的页面?

[英]In React Router V4, how to navigate to a page outside defined basename?

Consider the following React Router V4 code: 考虑以下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} />
                : window.location = "/auth/login" } <<-- ERROR HERE
    />;

class App extends Component {
    render() {
        return (
            <BrowserRouter basename="/editor">
                <Switch>
                    <AuthenticatedRoute exact path="/" component={AppHome} />
                    <AuthenticatedRoute
                        exact
                        path="/:module"
                        component={AppNav}
                    />

                    <Route component={PageNotFound} />
                </Switch>
            </BrowserRouter>
        );
    }
}

export default App;

auth/login is another ReactJs application that needs to be loaded from the server, or in other words, auth/login needs to bypass client routing and sent to server, where it will be routed and will serve a new ReactJS application ( auth application with login page). auth/login是需要从服务器加载另一个ReactJs应用,或者换句话说, auth/login需要绕过客户路由选择和发送到服务器,在那里将被路由,将成为一个新的ReactJS应用( auth应用与login页面)。

When trying to redirect (not authenticated) I´m getting the following error on browser console: 尝试重定向(未经身份验证)时,在浏览器控制台上出现以下错误:

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"

Looks like I´m "trapped" inside React Router V4 and nothing can get out of the basename. 看起来我被“困在” React Router V4内,没有任何东西会脱离基本名称。

How can I solve that and redirect to a server page, quitting or bypassing the react router itself ? 如何解决该问题并重定向到服务器页面,退出或绕过react路由器本身?

You need to install react-router-dom in your app 您需要在应用程序中安装react-router-dom

yarn add react-router-dom

or 要么

npm i -S react-router-dom

And import this 并导入这个

import { BrowserRouter as Router } from 'react-router-dom';

Where you want to redirect 您想重定向的地方

browserHistory.push('/path/some/where');

your code should be 您的代码应该是

import { BrowserRouter as Router } from 'react-router-dom';

const isAuthenticated = () => {
    let hasToken = localStorage.getItem("jwtToken");
    if (hasToken) return true;
    return false;
};

    const AuthenticatedRoute = ({ component: Component, ...rest, history }) =>
        <Route
            {...rest}
            render={props =>
                isAuthenticated()
                    ? <Component {...props} />
                    : history.push("/auth/login")}
        />;
class App extends Component {
    render() {
        return (
            <BrowserRouter basename="/editor">
                <Switch>
                    <AuthenticatedRoute exact path="/" component={AppHome} />
                    <AuthenticatedRoute
                        exact
                        path="/:module"
                        component={AppNav}
                    />

                    <Route component={PageNotFound} />
                </Switch>
            </BrowserRouter>
        );
    }
}

export default App;

You should use the Redirect component provided by react-router-dom 您应该使用react-router-dom提供的Redirect组件

import { Redirect, Route } from 'react-router-dom';
const AuthenticatedRoute = ({ component: Component, ...rest }) =>
  <Route
    {...rest}
    render={props =>
      isAuthenticated()? <Component {...props} />
       : (
          <Redirect
            to={{ pathname: 'auth/login' }}
           />
       )
     }
   />

Here is a working example 这是一个有效的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM