简体   繁体   English

为什么我的 React 应用程序没有在 localhost 中正确加载(损坏)?

[英]Why my React app is not loading correctly (broken) in localhost?

I had my react project working correctly in localhost.我让我的反应项目在 localhost 中正常工作。 Then, I decided to deploy it to github pages and it worked perfectly on the server too.然后,我决定将它部署到 github 页面,它也可以在服务器上完美运行。 Now, I'm trying to work on it again on localhost but it is not showing correctly.现在,我正在尝试在 localhost 上再次处理它,但它没有正确显示。 For some reason, photos are not loading and some css is not working correctly and after compile it in PowerShell says this:由于某种原因,照片没有加载,一些 css 工作不正常,在 PowerShell 编译后说:


Compiled successfully!

You can now view myportfolio in the browser.

Local: http://localhost:3000/myportfolio

On Your Network: http://192.168.56.1:3000/myportfolio

Note that the development build is not optimized.

To create a production build, use npm run build.

So if I go to my GitHub pages it is loading correctly but not in localhost (running npm start).因此,如果我将 go 到我的 GitHub 页面,它会正确加载,但不能在本地主机中加载(运行 npm 启动)。

Any suggestion?有什么建议吗? Thank you in advance and let me know if you need more clarification提前谢谢您,如果您需要更多说明,请告诉我

I did clone your repositories and found these problems:我确实克隆了您的存储库并发现了这些问题:

  1. You have been directly imported many third-party js given their relative path in the index.html.你已经直接导入了很多第三方js给定的index.html的相对路径。 That doesn't work.那是行不通的。 You should append %PUBLIC_URL% before them.你应该在他们之前 append %PUBLIC_URL% For eg例如

    <script src="%PUBLIC_URL%/js/jquery.flexslider.js"></script> and similary for other script files. <script src="%PUBLIC_URL%/js/jquery.flexslider.js"></script>和其他脚本文件类似。

  2. But even this is not the best that you can do.但即使这样也不是你能做到的最好的。 You must not try to use jquery or third party js in a React App.您不得尝试在 React App 中使用 jquery 或第三方 js。 Also, make it a part to install the related JS though npm and make them a part of the package.此外,通过npm使其成为安装相关 JS 的一部分,并使它们成为 package 的一部分。

  3. You'll have to use <img src={require('/public/images/background.png')}... (Btw, the image name on your gh-pages is different. It's logo.png there)if you want the webpack to compile and make it a part of your project.你必须使用<img src={require('/public/images/background.png')}... (顺便说一句,你的gh-pages上的图像名称不同。它是logo.png那里)如果你希望webpack编译并使其成为您项目的一部分。 Also, the path must reside within src and not public folder.此外,路径必须位于src而不是public中。

  4. Other errors are are related to keys .其他错误与keys相关。 Whenever you're mapping and iterating through a list in react you must specify a unique key.每当您在 react 中映射和迭代列表时,您必须指定一个唯一键。

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

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