为什么当我在 Apache 上运行我的 React 构建时......只出现空白页面......?

IT技术 javascript reactjs apache
2021-05-09 00:09:00

应用程序在节点服务器中运行良好,但即使我使用了绝对路径,我在 Apache 服务器中也出现了空白页面。

这是 index.html 代码

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  <meta name="theme-color" content="#000000">
  <link rel="manifest" href="/manifest.json">
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <title>React App</title>
  <link href="./static/css/main.0778317d.css" rel="stylesheet">
</head>

<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div id="root"></div>
  <script type="text/javascript" src="./static/js/main.1b4d826e.js"></script>
</body>

以下是截图。。 在此处输入图片说明

在此处输入图片说明

3个回答

我正在使用react-router 4因此,当我使用路由器时,它会产生问题并且无法使用绝对路径。而不是Router使用BrowserRouter并使用basename用于文件夹商店,我在其中复制了我的项目的构建。

从'react-router-dom'导入{ BrowserRouter, Route, Switch };

<BrowserRouter history={history} basename="/shop/">
      <div>
        <Switch>
          <Route exact path="/" component={App} />
          <Route path="/cart" component={Cart} />
          <PublicRoute path="/login" component={LoginPage} />
          <PrivateRoute path="/dashboard" component={DashBoardPage} />
          <PrivateRoute path="/checkout" component={checkOut} />
          <PrivateRoute path="/favourite" component={favourite} />
          <PrivateRoute
            path="/payment_Information"
            component={PaymentInformation}
          />
          <Route path="/product_details/:pid" component={ProductDeatils} />
          <Route path="*" compononent="NotFound" />
        </Switch>
      </div>
    </BrowserRouter>

并在package.json文件"homepage": "/shop"中创建路径。

即使尝试了@Abdul Moiz 提到的步骤,我也无法使用 React Router v4 使我的应用程序在 Apache 托管上工作。

我找到了这个答案:https : //stackoverflow.com/a/50657757/7867822

基本上改变<BrowserRouter/>,以<HashRouter/>适合我的工作。

如果网站托管在http://namweb.com/NamFdr
在以下配置中添加文件夹名称“NamFdr”:

|+> 在 package.json 中添加

"homepage": "/NamFdr",

|+> 在 index.js 中添加路由器基名配置

ReactDOM.render(
    <BrowserRouter basename='/blocal'>
        <App />
    </BrowserRouter>,
    document.getElementById('root')
)

[或] 在 app.js 中添加 Router basename 配置

import { BrowserRouter, Route, Switch } from 'react-router-dom';

-------------------------

<BrowserRouter basename="/NamFdr/">
    <Switch>
        <Route exact path="/" component={App} />


    </Switch>
</BrowserRouter>