gpt4 book ai didi

javascript - 使用 Webpack、React、Babel 组合时,React 代码未运行

转载 作者:行者123 更新时间:2023-11-30 15:02:22 27 4
gpt4 key购买 nike

我一直在关注 Petr Tichy (@ihatetomatoes) Webpack 2 教程系列,并且已经访问了 this vid 包括安装 React 和 Babel。

我已经用细齿梳仔细检查了代码,并花了几个小时在谷歌上搜索,但我就是无法让我的代码以预期的方式执行。我的 app.js 文件中的 React 方法没有执行。

应该将“Hello World”渲染到索引页面的根元素中,但我什么也没得到。

这就是我所拥有的...

./src/app.js

const css = require("./styles.scss");

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);

./src/index.html

<html>
<head>
<meta charset="UTF-8">
<title><%= htmlWebpackPlugin.options.title %></title>
<body>
<div id="root"></div>
</body>
</html>

webpack.config.js

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

const path = require('path');

module.exports = {
entry: './src/app.js',
output: {
path: path.resolve(__dirname, "dist"),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.scss$/,
use: ExtractTextPlugin.extract( {
fallback: 'style-loader',
use: ['css-loader','sass-loader']
} )
},
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
}
]
},
plugins: [
new HTMLWebpackPlugin({
title: "IWTE",
template: './src/index.html',
minify: {
collapseWhitespace: true,
},
hash: true
}),
new ExtractTextPlugin("styles.css")
],
devServer: {
contentBase: "./dist",
compress: true,
hot: true,
open: true
},
}

.babelrc

{
"presets": ["es2015","react"]
}

包.json

{
"name": "creator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "MIT",
"private": true,
"scripts": {
"dev": "webpack-dev-server --progress --colors",
"prod": "webpack -p"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"eslint": "^4.7.1",
"extract-text-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^2.30.1",
"node-sass": "^4.5.3",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.8.2"
},
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1"
}
}

非常感谢任何关于调试此路径的建议或意见!我是前端开发人员的相对菜鸟。谢谢!

最佳答案

在你的 package.json 里面做一个改变:

"scripts": {
"dev": "webpack-dev-server --hot --progress --colors",
"prod": "webpack -p"
}

您需要在开发命令中添加--hot 以启用热重载。

如果它不起作用,请告诉我。干杯!

关于javascript - 使用 Webpack、React、Babel 组合时,React 代码未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com