簡體   English   中英

無法在Github Pages上部署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 ,與我從master分支的build文件夾復制的styles.csstransformd.js一起存儲在gh-pages分支 gh-pages分支上沒有其他內容。 當我導航到https://krasnokutskiyea.github.io/myProj/時,我只看到Hello gh-pages !,但是我的實際react應用程序沒有注入div id =“ app” 控制台中沒有錯誤。 有什么想法如何解決嗎? 我來自master分支的package.json:

{
  "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'),
)

您的path="/" Route將不起作用,因為您的網頁的基礎是/myProj

您可以通過將BrowserRouterbasename/myProj來解決此/myProj

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

問題出在您的路由器/路徑上。

您的React應用正在渲染,但是您的單個路由與您的網址路徑不匹配:

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

由於您的應用程序位於/myProj/上,因此/myProj/

將路由路徑更改為/myProj/解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM