无法在 Github 页面上部署 React App。应用未注入 <div id="app"/>

Can't deploy React App on Github Pages. App doesn't get injected into <div id="app"/>

<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
 <title>My App</title>
 <link href="styles.css" rel="stylesheet"></head>
<body>
  <p>Hello gh-pages!</p>
  <div id="app"></div>
  <script type="text/javascript" src="transformed.js"></script>
</body>
</html>

这是我的 index.html,与 styles.css 和 [= 一起存储在 gh-pages 分支上23=]transformed.js 我从 master 分支上的构建文件夹中复制了它。 gh-pages 分支上没有其他内容。当我导航到 https://krasnokutskiyea.github.io/myProj/ 时,我只看到 Hello gh-pages!,但我的实际 React 应用程序没有被注入 div id="app".控制台中没有错误。任何想法如何解决它? 我的 package.json 来自 master 分支:

{
  "name": "newapp",
  "version": "1.0.0",
  "description": "myfirstapp",
  "main": "index.js",
  "homepage": "https://krasnokutskiyea.github.io/myProj",
  "scripts": {
    "build": "webpack",
    "build:stats": "webpack --env production --json > stats.json",
    "start": "webpack-dev-server",
    "lint": "eslint .",
    "deploy": "gh-pages -d build"
  },
  "author": "evgeny krasnokutskiy",
  "license": "ISC",
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1",
    "react-redux": "^5.0.5",
    "react-router": "^4.2.0",
    "react-router-dom": "^4.2.2",
    "redux": "^4.0.0",
    "redux-act": "^1.3.2",
    "redux-form": "^7.1.0",
    "redux-saga": "^0.16.0",
    "superagent": "^3.6.0"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-eslint": "^8.0.0",
    "babel-loader": "^7.0.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-1": "^6.24.1",
    "css-loader": "^0.28.5",
    "eslint": "^4.19.1",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-plugin-import": "^2.12.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-react": "^7.4.0",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "gh-pages": "^1.2.0",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.21.0",
    "webpack": "^4.12.0",
    "webpack-cli": "^3.0.8",
    "webpack-dev-server": "^3.1.4"
  }
}

Webpack 配置:

const HTMLWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const path = require('path')

const HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
  template: path.join(__dirname, '/app/index.html'),
  filename: 'index.html',
  inject: 'body',
})

module.exports = {
  entry: ['babel-polyfill', path.join(__dirname, '/app/index.js')],
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
      }, {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader?modules,localIdentName="[name]-[local]-
[hash:base64:6]"',
        }),
      }],
  },
  output: {
    filename: 'transformed.js',
    path: path.join(__dirname, '/build'),
    publicPath: '/',
  },
  devServer: {
    historyApiFallback: true,
  },
  plugins: [
    HTMLWebpackPluginConfig,
    new ExtractTextPlugin('styles.css'),
  ],
  mode: 'production',
}

我的app/index.js:

/* eslint-env node */

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, compose, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import createSagaMiddleware from 'redux-saga'
import { BrowserRouter, Route } from 'react-router-dom'

import allReducers from './reducers/index'
import RootSaga from './sagas/index'
import EntryPage from './containers/EntryPage'


// CREATING STORE AND SAGA
const saga = createSagaMiddleware()
const composeEnchancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
const store = createStore(allReducers, 
composeEnchancers(applyMiddleware(saga)))
saga.run(RootSaga)



// RENDERING ROOT COMPONENT
ReactDOM.render(

  <Provider store={store}>
    <BrowserRouter>
      <div>
        <Route exact path="/" component={EntryPage} />
      </div>
    </BrowserRouter>
  </Provider>,

  document.getElementById('app'),
)

问题出在你的router/path。

您的 React 应用程序正在呈现,但您的单一路线与您的 url 路径不匹配:

<Route exact path="/" component={EntryPage} />

这行不通,因为您的应用在 /myProj/

将您的路线路径更改为 /myProj/ 即可解决问题。

您的 Routepath="/" 将无法正常工作,因为您的网页基础是 /myProj

您可以通过给您的 BrowserRouter 一个 /myProjbasename 来解决这个问题:

ReactDOM.render(
  <Provider store={store}>
    <BrowserRouter basename="/myProj">
      <div>
        <Route exact path="/" component={EntryPage} />
      </div>
    </BrowserRouter>
  </Provider>,
  document.getElementById('app'),
)