gpt4 book ai didi

javascript - 目标容器不是使用 webpack 4 和 React 的 DOM 元素

转载 作者:行者123 更新时间:2023-12-01 01:18:49 25 4
gpt4 key购买 nike

我正在使用 Webpack 4.29.0 和 React 16.7.0,并且我正在尝试从 2019 年 Udemy 类(class) Web 开发从开始到结束呈现一条简单的网站消息,但是我收到错误:未捕获错误:目标容器不是 DOM 元素,我无法确定问题所在,并且我对 webpack.config.js 所做的每项更改似乎都不起作用。

我的理解:在 src 文件夹中>我创建了组件文件夹> header.js>导入react并导出将由>index.js使用的 header 组件>使用ReactDom.render渲染它,然后使用index.html>#root显示。我不明白为什么我的页面信息无法加载。

Package.json 信息:

{
"name": "ch-hawk-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"buildDev": "webpack-dev-server --mode development",
"buildProd": "webpack-dev-server --mode production",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/konistleio/Ch-Hawk-project.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/konistleio/Ch-Hawk-project/issues"
},
"homepage": "https://github.com/konistleio/Ch-Hawk-project#readme",
"devDependencies": {
"clean-webpack-plugin": "^1.0.1",
"css-loader": "^2.1.0",
"html-webpack-plugin": "^3.2.0",
"mini-css-extract-plugin": "^0.5.0",
"node-sass": "^4.11.0",
"sass-loader": "^7.1.0",
"webpack": "^4.29.0",
"webpack-cli": "^3.2.1",
"webpack-dev-server": "^3.1.14"
},
"dependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.5",
"optimize-css-assets-webpack-plugin": "^5.0.1",
"react": "^16.7.0",
"react-dom": "^16.7.0",
"terser-webpack-plugin": "^1.2.1"
},

"babel": {
"presets": [

"@babel/preset-env",
"@babel/react"

]
}
}

我的文件结构是

My File Structure

在我的 webpack.config.js 中,我有以下配置

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCss = require("optimize-css-assets-webpack-plugin");

module.exports = {

entry: {
app: './src/index.js'
},

optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true
}
}

},

minimizer: [
new TerserPlugin({
parallel: true,
terserOptions: {
ecma: 6
}
}),

new OptimizeCss({
cssProcessorOptions: {
discardComments: true
}

})
],

},



devServer: {
hot: true,
compress: true,
contentBase: path.join(__dirname, 'dist'),
open: 'Chrome'
},
watch: true,
devtool: 'source-map',
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},

plugins: [
new CleanWebpackPlugin(['dist']),
new MiniCssExtractPlugin({
filename: "style.css",
chunkFilename: "[name].css"

}),

new webpack.HotModuleReplacementPlugin(),
new HtmlWebpackPlugin({
templates: './src/index.html'
})

],

module: {
rules: [{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader"

},
"sass-loader"
]


},

{
test: /\.js$/,
exclude: /node_modules/,
use: ['babel-loader']
}

]

},

resolve: {

extensions: [
'.js',
'.scss'
]
}


};

在 src 文件夹内,我有包含 header.js 的组件文件夹

import React from 'react';


export default class Header extends React.Component {

render() {

return (
<h1>{this.props.title}</h1>
)
}
}

在 src 文件夹中我还有 index.html

<html>
<head>
<title>
Our APP
</title>

</head>
<body>
<div id="root">


</div>


<script src="/bundle.js"></script>
</body>
</html>

在 src 文件夹中我还有 index.js

import './sass/bootstrap/scss/bootstrap.scss';
import React from 'react';
import ReactDOM from 'react-dom';
import Header from './components/header';


ReactDOM.render (
<Header title= "Our custom message" />,

document.getElementById('root')
)

谷歌浏览器出错

Google Chrome Error React

Webpack 中没有错误

Webpack information

最佳答案

您的 webpack.config.js 文件中有拼写错误。将 templates 更改为 template 就可以了!

new HtmlWebpackPlugin({
template: './src/index.html'
})

React 无法在 HTML 文档中找到根元素。这就是为什么它会引发这样的错误。

关于javascript - 目标容器不是使用 webpack 4 和 React 的 DOM 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54455647/

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