简体   繁体   English

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

[英]ReactJS code compiled successfully , but not printing on Browser

After a lot of efforts i was able to install and configure ReactJS.经过大量努力,我能够安装和配置 ReactJS。 After executing "npm start" command, the code was "COMPILED SUCCESSFULLY" and redirected me to the web browser.执行“npm start”命令后,代码为“COMPILED SUCCESSFULLY”并将我重定向到网络浏览器。 But there wasn't any output , ie., the browser was " blank ".但是没有任何输出,即浏览器是“空白”。 Can anyone resolve this??谁能解决这个问题?? ( my first post here ) (我在这里的第一篇文章)

Also check the code that i have used..还要检查我使用过的代码..

index.html file 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应用程序.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 snippet package.json 片段

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

webpack.config.js 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'
      })
   ]
}

The only issue is the output is not getting displayed on browser..唯一的问题是输出没有显示在浏览器上..

command prompt COMMAND PROMPT AFTER "npm start"命令提示符“npm start”后的命令提示符

browser output not displaying浏览器输出不显示

Add an .babelrc file in root folder with following:在根文件夹中添加一个.babelrc文件,内容如下:

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

And package.json is expected to include following dependencies:并且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"
  }

Update更新

Also update webpack.config.js to: 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"
    })
  ]
};

Check the path and change Component to component .检查路径并将Component更改为component

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack编译成功,但项目未在浏览器中加载 - Webpack compiled successfully but project does not load in browser PDF 使用 ReactJS MySql 生成成功但未在浏览器中显示为可下载 - PDF generated successfully but not showing as downloadable in the browser using ReactJS MySql reactjs代码未显示在浏览器中 - reactjs code not showing up in browser React.js新代码未呈现到浏览器 - Reactjs new code not rendering to the browser 代码成功运行,但未在浏览器中显示输出 - Code running successfully but not showing output in the browser 我的终端正在读取 webpack 编译成功,但连接到 react-dom 后浏览器页面为空 - My terminal is reading webpack compiled successfully but browser page is empty after connecting to react-dom /Reactjs 在浏览器中获取 405 状态码 - /Reactjs fetching bringing a 405 status code in browser 为什么webpack编译成功,但编译输出中不存在我的源代码? - Why does webpack compile successfully but my source code does not exist in the compiled output? 在Web浏览器环境中执行时,JavaScript是否编译为机器代码? - Is JavaScript compiled to machine code when executed in a Web Browser Environment? Web浏览器上Java,ReactJs代码的服务器端渲染 - Server-side rendering of Java, ReactJs code on a web browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM