简体   繁体   English

react-router-dom BrowserRouter在构建后无法正常工作

[英]react-router-dom BrowserRouter not working after build

This is the first time I tried react. 这是我第一次尝试反应。 I create a project using create-react-app , then for routing, I use React Router V4 for web react-router-dom . 我使用create-react-app创建一个项目,然后用于路由,我使用React Router V4进行web react-router-dom

This is my index.js file 这是我的index.js文件

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';
import './index.css';

ReactDOM.render(
  <Router>
    <App />
  </Router>,
  document.getElementById('root')
);

There's no problem when I start the server with npm start . 使用npm start启动服务器时没有问题。 But when I build the app with npm run build , putting it in my www folder in my Apache server and access it, I only get empty page with <!-- react-empty: 1 --> inside my root div . 但是当我使用npm run build构建应用程序时,将它放在我的Apache服务器的www文件夹中并访问它,我只能在我的root div获得带有<!-- react-empty: 1 -->空白页面。

<div id="root">
  <!-- react-empty: 1 -->
</div>

I have tried looking for similar cases, but the answer always about adding historyApiFallback in webpack.config.js or gulp, which I can't find anywhere in the create-react-app template. 我试图寻找类似的情况,但总是回答有关添加historyApiFallback在webpack.config.js或一饮而尽,我不能在任何地方找到create-react-app模板。

I have tried HashRouter , it's working fine, but there's /#/ in the url which is undesirable. 我已经尝试过HashRouter ,它运行正常,但是url中的/#/是不可取的。

How can I get the BrowserRouter to work after I build the app? 在构建应用程序后,如何让BrowserRouter工作?

Well, there was no problem at all. 嗯,完全没有问题。 It worked, even if it was built using npm run build . 它工作,即使它是使用npm run build My best guess is: you placed the files in the wrong folder. 我最好的猜测是:您将文件放在错误的文件夹中。

When you build your code using npm run build , the static folder should be placed inside the MallApp folder. 使用npm run build构建代码时, static文件夹应放在MallApp文件夹中。 So your folder structure should be like this: 所以你的文件夹结构应该是这样的:

/var/www/mywebfolder/index.html
/var/www/mywebfolder/MallApp/static

Check your browser's console, I believe it contains errors. 检查浏览器的控制台,我认为它包含错误。 Failed to get the js and css files. 无法获取js和css文件。

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

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