简体   繁体   English

React-Router-DOM 实际上没有创建路由?

[英]React-Router-DOM not actually creating routes?

I followed a tutorial to set up Auth0 on a simple create-react-app.我按照教程在一个简单的 create-react-app 上设置Auth0 The only thing I have in the project as of now is authentication and routing.到目前为止,我在项目中唯一拥有的是身份验证和路由。 When developing in localhost , the app worked fine:localhost 中开发时,该应用程序运行良好:

  • / routed to /home , and showed the current signed-in status / 路由到/home ,并显示当前登录状态
  • /home routed to /home , ^ /home 路由到/home , ^
  • /callback, called by the authentication service, routed fine and handles the authentication and redirects to a logged in page. /callback 由身份验证服务调用,路由良好并处理身份验证并重定向到登录页面。

As I mentioned, all of this routing worked fine on development.正如我所提到的,所有这些路由在开发中都运行良好。 I was able to see a login page, press login, login with credentials, get rerouted through /callback back to the homepage and see material only available if you're logged in. I was then able to log out and be routed correctly again back to the login page.我能够看到登录页面,按登录,使用凭据登录,通过/callback重新路由回主页,并查看仅在您登录时可用的材料。然后我能够注销并再次正确路由回来到登录页面。

I deployed the app to a website ( https://dashboard.xxxxx.com ) but none of the routes work except /.我将该应用程序部署到了一个网站 ( https://dashboard.xxxxx.com ),但除了 / 之外,没有任何路由可以工作。 So https://dashboard.xxxxx.com works, but https://dashboard.xxxxx.com/home nor https://dashboard.xxxxx.com/callback cannot be found.所以https://dashboard.xxxxx.com有效,但找不到https://dashboard.xxxxx.com/homehttps://dashboard.xxxxx.com/callback

This causes the auth0 service to fail logging in because it redirects to https://dashboard.xxxxx.com/callback but that page does not exist.这会导致auth0服务无法登录,因为它重定向到https://dashboard.xxxxx.com/callback但该页面不存在。

I started off using Router from react-router-dom with just Routes, then I tried Router with a Switch and Routes.我开始只使用带有路由的react-router-dom ,然后我尝试使用带有交换机和路由的路由器。 I then tried BrowserRouter and then HashRouter , but none have worked.然后我尝试了BrowserRouterHashRouter ,但都没有奏效。 My current code is below:我当前的代码如下:

    <HashRouter history={history} component={Home}>
        <Switch>
            <Route exact path='/' render={(props) => <Home auth={auth} {...props}/>}/>
            <Route exact path='/home' render={(props) => <Home auth={auth} {...props}/>}/>
            <Route exact path='/callback' render={(props) => {
                handleAuth(props);
                return <Callback {...props}/>
            }}/>
        </Switch>
    </HashRouter>

This is happening because you are not redirecting all requests to index.html from server which is used to initialise your application.发生这种情况是因为您没有将所有请求从用于初始化您的应用程序的服务器重定向到index.html You need to redirect all call to index.html from server side may be in .htaccess file, node or any other framework you would be using.您需要将所有调用从服务器端重定向到 index.html 可能位于.htaccess文件、节点或您将使用的任何其他框架中。

Locally the redirection to index.html is handled by Create-react-app if you are using that or any other way.如果您正在使用 Create-react-app 或任何其他方式,则本地重定向到index.html由 Create-react-app 处理。

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

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