ReactJS代码已成功编译,但无法在浏览器上打印

时间:2019-07-16 07:00:25

标签: javascript html node.js reactjs

经过大量的努力,我得以安装和配置ReactJS。执行“ npm start”命令后,代码为“ COMPILED SUCCESSFULLY”,并且将我重定向到Web浏览器。 但是没有任何输出,即浏览器为“ 空白”。 谁能解决这个问题? (我的第一篇文章)

还要检查我使用的代码。

index.html 文件

<!DOCTYPE html>
<html lang = "en">
   <head>
      <meta charset = "UTF-8">
      <title>React App</title>
   </head>
   <body>
      <div id = "app"></div>
      <script src = 'index_bundle.js'></script>
   </body>
</html>

App.js

import React, { component } from 'react';
class App extends Component {
    render(){
        return(
        <div>
        <h1> Hello World</h1>
        <p>Hello </p>   
        </div>
        );
    }
}
export default App;

main.js

import React from 'react';
import { render } from 'react-dom';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));

package.json代码段

"start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
   entry: './main.js',
   output: {
      path: path.join(__dirname, '/bundle'),
      filename: 'index_bundle.js'
   },
   devServer: {
      inline: true,
      port: 8001
   },
   module: {
      rules: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   },
   plugins:[
      new HtmlWebpackPlugin({
         template: './index.html'
      })
   ]
}

唯一的问题是输出未显示在浏览器上

命令提示符 COMMAND PROMPT AFTER "npm start"

浏览器 output not displaying

3 个答案:

答案 0 :(得分:1)

在根文件夹中添加一个.babelrc文件,内容如下:

{
    "presets": ["@babel/preset-env", "@babel/preset-react"]
}

并且package.json应该包含以下依赖项:

  "devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "react": "^16.8.5",
    "react-dom": "^16.8.5",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  }

更新

也将webpack.config.js更新为:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./main.js",
  output: {
    path: path.join(__dirname, "/bundle"),
    filename: "index_bundle.js"
  },
  devServer: {
    inline: true,
    port: 8001
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html"
    })
  ]
};

答案 1 :(得分:0)

如果要为插件创建html,则应将filename添加到配置中,并且其路径应相对于根目录。另外,如果您想将标题更改为“ React App”,则还必须进行设置。

在您的html中添加此

<title><%= htmlWebpackPlugin.options.title %></title>

和您的插件设置

 new HtmlWebpackPlugin({
            title: 'React App',
            template: './index.html',
            filename: './yourPath/index.html' //relative to root of the application
})

答案 2 :(得分:0)

检查路径并将Component更改为component

import React, { Component } from 'react';
class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Route path="/" component={HomePage} exact  />
        </div>
      </Router> 
    );
  }
}