【问题标题】:ReactJS code compiled successfully , but not printing on BrowserReactJS 代码编译成功,但没有在浏览器上打印
【发布时间】:2019-11-24 20:39:36
【问题描述】:

经过大量努力,我能够安装和配置 ReactJS。执行“npm start”命令后,代码“成功编译”并将我重定向到网络浏览器。 但是没有任何输出,即浏览器是“blank”。 谁能解决这个问题?? (我在这里的第一篇文章)

还要检查我使用的代码..

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;

ma​​in.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 sn-p

"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

【问题讨论】:

  • 浏览器的控制台有错误吗?

标签: javascript html node.js reactjs


【解决方案1】:

检查路径并将Component更改为component

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

【讨论】:

    【解决方案2】:

    在根文件夹中添加.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"
        })
      ]
    };
    
    

    【讨论】:

    • ,babelrc 已经存在...代码和一切都是正确的,但不明白为什么它不执行
    • 你能分享你的代码来检查一下,因为上面的代码确实对我有用。
    猜你喜欢
    • 2017-10-22
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-14
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多