简体   繁体   English

将 React 应用程序发布到 Github 页面的问题

[英]Issue with publishing React app to Github pages

Okay, so when I run npm run deploy, all steps followed, it says published... great.好的,所以当我运行 npm run deploy 时,所有步骤都遵循了,它说已发布......很棒。

Check Github hosted link and weirdly its literally only showing JUST my navbar component, nothing else.检查 Github 托管链接,奇怪的是它实际上只显示我的导航栏组件,没有别的。 As you can see by the router in the app.js below, it should be showing the navbar element and the RecipeList component.正如您在下面 app.js 中的路由器所看到的,它应该显示导航栏元素和 RecipeList 组件。 Manually typing routes results in 404.手动输入路由会导致 404。

app.js - app.js -

import React from 'react';
import './App.css';
import Navbar from './Component/Navbar/Navbar';
import RecipeList from './Component/RecipeList/RecipeList';
import { Switch, Route, BrowserRouter } from 'react-router-dom'
import RecipeItemDetails from './Component/RecipeItemDetails/RecipeItemDetails';



function App() {
  return (
    <BrowserRouter>
    <Switch>
    <div className="App">
      <Navbar/>
      <Route exact path="/" component={RecipeList} />
      <Route path="/recipes" component={RecipeList} />
      <Route path="/recipes/:id" component={RecipeItemDetails} />
    </div>
    </Switch>
    </BrowserRouter>
  );
}

export default App;

package.json - package.json -

{
  "name": "gmcb-react",
  "version": "0.1.0",
  "private": true,
  "homepage": "http://Wrecket.github.io/GMCB-react-conversion",
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.3",
    "save-dev": "0.0.1-security"
  },

Can anyone tell me what amateur mistake im likely making?谁能告诉我我可能会犯什么业余错误?

I think this article will help which explains a similar problem to which I had deploying against one of my project's ghpage .我认为这篇文章将有助于解释我针对我的一个项目的ghpage部署的类似问题。

Basically, you need to take advantage of <BrowserRouter/> 's basename property for the <Routes/> to work as they do locally up on ghpages, specifically settings the value to;基本上,您需要利用<BrowserRouter/>的 basename 属性让<Routes/>像在 ghpages 上本地一样工作,特别是将值设置为;

basename={process.env.PUBLIC_URL}

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

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