gpt4 book ai didi

javascript - ReactJS 不渲染到 div

转载 作者:行者123 更新时间:2023-12-03 00:23:02 26 4
gpt4 key购买 nike

我尝试运行我的应用程序,但控制台中没有任何内容,窗口中也没有任何内容,我尝试添加 <script src="target/app.js"></script>但它出现错误 Cannot GET /target/app.js 。我也在搜索工作怎么样create-react-app ,因为它没有在 html 文件中使用任何带有捆绑的脚本,但没有效果

这是webpack.config.js 。但我不认为 webpack 有问题。或者可能是 devServer 有问题?

const path = require('path')
const webpack = require('webpack')

module.exports = {
entry: {
app: ['@babel/polyfill', './src/index.js']
},
module: {
rules: [
{
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader']
},
{
test: /\.(js|jsx)?$/,
include: /node_modules/,
use: ['react-hot-loader/webpack'],
},
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
loader: require.resolve('babel-loader'),
},
{
test: /\.(js|jsx)$/,
loader: 'prettier-loader',
exclude: /node_modules/,
options: require('./prettier.config')
},
{
test: /\.(jpe?g|png|gif)$/,
use: [{
/* inline if smaller than 10 KB, otherwise load as a file */
loader: 'url-loader',
options: {
limit: 10000
}
}]
},
{
test: /\.(eot|svg|ttf|woff2?|otf)$/,
use: 'file-loader'
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx', '.css', 'scss']
},
output: {
path: path.resolve(__dirname, '/target/'),
filename: '[name].js',
chunkFilename: '[name].chunk.js',
publicPath: '/public/'
},
plugins: [
new webpack.ProvidePlugin({
R: 'ramda'
})
],
devServer: {
contentBase: path.join(__dirname, '/public'),
port: 3131
}
}

这是src/index.js 。在这里我没有看到任何错误,一切都应该正常工作。

import React from 'react'
import {render} from 'react-dom'
const App = () => (
<div>
<h1>Hello world!!</h1>
</div>
)
render(<App />, document.getElementById('root'))

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8">
<meta name=”viewport” content=”width=device-width, initial-scale=1.0">
<title>React</title>
</head>
<body>
<div id=”root”></div>
</body>
</html>

最佳答案

问题似乎是 webpack 配置问题,请检查此修复是否有效。

output: {
path: path.resolve(__dirname, 'dist/'),
publicPath: '/dist/',
filename: 'bundle.js'
},
devServer: {
contentBase: path.join(__dirname, 'public/'),
port: 3000,
publicPath: 'http://localhost:3000/dist/'
},

关于javascript - ReactJS 不渲染到 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54221533/

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