【问题标题】:Route Param not working with React after deploying react application to apache server将 React 应用程序部署到 Apache 服务器后,Route Param 无法与 React 一起使用
【发布时间】:2021-03-25 10:40:10
【问题描述】:

我已经创建了 React 应用程序并在我的应用程序中使用了 react-router。我在 Route 中使用了路由器参数。每当我尝试在本地访问时,它都可以正常工作,但是当我部署到 apache 服务器时,路由参数链接不起作用。请帮帮我。

我的 React 路由器代码:

<BrowserRouter basename="tryloka_stg_app">
      <Switch>
        <Route exact path="/" component={Home} />

        <Route exact path="/partner" component={Partner} />
        <Route exact path="/signup" component={SignUp} />
        <Route exact path="/signupOTP" component={SignUpOTP} />
        <Route exact path="/signupBirthday" component={SignupBirthday} />
         <Route exact path="/confirmPassword/:id" component={ConfirmPassword} />
  

        <Route component={NotFound} />
      </Switch>
    </BrowserRouter>

每当我尝试在生产环境中访问http://35.181.69.171/tryloka_stg_app/confirmPassword/14 时,它都会显示一个没有错误的空白页面。但是当我像localhost:3000/tryloka_stg_app/confirmPassword/14 这样访问本地主机时,它工作正常。 服务器中的构建文件位置是 /var/www/html/tryloka_stg_app

我的 .htaccess 文件:

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

我的 package.json 文件:

{
  "name": "tryloka",
  "homepage": ".",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@date-io/date-fns": "^2.10.8",
    "@fortawesome/fontawesome-free": "^5.15.2",
    "@fortawesome/react-fontawesome": "^0.1.14",
    "@material-ui/pickers": "^3.3.10",
    "@syncfusion/ej2-react-calendars": "^18.4.48",
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.2",
    "@testing-library/user-event": "^12.2.2",
   
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

【问题讨论】:

  • 您是否尝试过在 package.json 中更改您的主页?
  • 是的,我已将其更改为35.181.69.171/tryloka_stg_app,但仍显示空白页@ShyamKumar

标签: javascript reactjs react-router


【解决方案1】:

将 React 应用部署到子目录

将基本名称添加到您已经完成的路线

相应修改package.json的首页

“主页”:“http://35.181.69.171/tryloka_stg_app”,

【讨论】:

  • 你能把答案标记为正确吗?
猜你喜欢
  • 2017-08-04
  • 2020-02-13
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
  • 2020-12-09
  • 2021-05-22
  • 2020-04-18
  • 2020-05-03
相关资源
最近更新 更多