如何为 React JS 应用程序创建 _redirects 文件以将其部署在 Netlify 上

How to create _redirects file for React JS app to deploy it on Netlify

我需要一些关于如何创建 _redirects 文件以使我网站的其他页面正常工作的帮助。它正在使用反应路由器。你能告诉我一个我应该怎么做的例子吗?网址使用参数和查询。

<Router basename = {process.env.PUBLIC_URL}>
      
      <Switch>
          <Route exact path = "/">
          <Root/>
          </Route>
          <Route exact path = {"/Pesquisar/:searchField/:page"} component = {withRouter(Pesquisa)}>
          </Route>
          <Route exact path = {"/documento/:id"} component = {Documento}>
          </Route>
          <Route exact insecure component={ Insecure }/>

      </Switch>
    </Router>
    );
}

这是 App.js 文件,它正在使用 react-router。 _redirects 应该是什么样的?

您可以使用 HashRouter 而不是 BrowserRouter,因此您不需要那些重定向文件。实际上,如果您的 url 包含 #.

,您的 React 应用程序导航将在 Netlify 上运行

所以只写:

import { HashRouter, Switch, Route, Redirect } from "react-router-dom";

function App(){
  return (
    <HashRouter>
      <Switch>
          <Route exact path="/"/>
          <Route path="/Pesquisar/:searchField/:page" component={withRouter(Pesquisa)} />
          <Route path="/documento/:id" component={Documento}/>
          <Route insecure component={Insecure}/>

      </Switch>
    </HashRouter>
    );
  )
}