【问题标题】:React app only displays the background color when deployed to github pagesReact 应用程序仅在部署到 github 页面时显示背景颜色
【发布时间】:2020-12-20 12:16:08
【问题描述】:

我在 github 页面上部署了一个 react 应用程序,但它只显示了我的背景颜色,没有显示任何实际组件。我按照本指南 (https://create-react-app.dev/docs/deployment/#github-pages) 运行终端命令并在我的 json 文件中插入正确的代码。我在部署时没有收到任何错误,当我检查页面的控制台时,它没有显示任何错误。

我的主页作为 github 页面链接设置在名称下方。

这是我的 package.json 中的脚本

  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },

这是我的 app.js

import React from 'react';
import Navbar from './components/Navbar';
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';
import './App.css';
import Home from './components/pages/Home';

function App() {
  return (
    <React.Fragment>
      <Router>

        <Switch>
          <Route path='/' exact component = {Home} />
        </Switch>
      </Router>
    </React.Fragment>
  );
}

export default App;

我的 ReactDOM 有问题吗?一切都在本地工作。我不确定可能是什么问题。

【问题讨论】:

  • 你能在部署前检查构建文件夹吗?否则尝试以正常方式部署,例如在导航到我们的构建目录后使用git push origin master

标签: reactjs github-pages react-dom


【解决方案1】:

RoutepathRouter basename 本身,您应该添加process.env.PUBLIC_URL 作为指向您地址的指针

  <Router basename={process.env.PUBLIC_URL}>

【讨论】:

    猜你喜欢
    • 2021-08-25
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多