React Router Dom Build 问题,我总是能看到我的主页。我已经尝试过 exact

React Router Dom Problem on Build, im always getting my main page. I already tried with exact

我使用 create-react-app 创建了我的应用程序,安装了 react-router-dom 并且在开发过程中它运行良好。但是当我构建时,我无法导航。我总是得到我的主页 ()。我将它上传到 apache 服务器。

这是我的 .htaccess:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

这是浏览器路由器:

      <BrowserRouter>
          <GlobalStyle/>
          <NavBar/>
          <Switch>
            <Route path="/agente/:dni">
              <Landing/>
            </Route>
            <Route path="/">
              <ErrorPage/>
            </Route>
          </Switch>
          <Footer/>
      </BrowserRouter>

我的主页是“https://******/agenda-landing”。 Agenda-landing 是构建产品所在的文件夹。

更新:

我现在尝试使用 HashRouter,它成功了。但是我不喜欢在 URL 中有一个 '#' 所以,有什么建议可以让它与 BrowserRouter 一起工作吗?

你可以查看路由器的 basename prop.

basename

The base URL for all locations. If your app is served from a sub-directory on your server, you’ll want to set this to the sub-directory. A properly formatted basename should have a leading slash, but no trailing slash.

<BrowserRouter basename="/calendar">
    <Link to="/today"/> // renders <a href="/calendar/today">
    <Link to="/tomorrow"/> // renders <a href="/calendar/tomorrow">
    ...
</BrowserRouter>
<GlobalStyle/>
<BrowserRouter basename="/agenda-landing"> // <-- add router basename
  <NavBar/>
  <Switch>
    <Route path="/agente/:dni">
      <Landing/>
    </Route>
    <Route path="/">
      <ErrorPage/>
    </Route>
  </Switch>
  <Footer/>
</BrowserRouter>