Javascript 反应+;我做错了什么
我试图得到一个非常简单的react+网页包外壳 一切似乎都在正确地捆绑和编译,但当我运行应用程序时,不会呈现react 我的网页配置Javascript 反应+;我做错了什么,javascript,reactjs,webpack,Javascript,Reactjs,Webpack,我试图得到一个非常简单的react+网页包外壳 一切似乎都在正确地捆绑和编译,但当我运行应用程序时,不会呈现react 我的网页配置 module.exports = { entry: [ './src/index.js' ], module: { rules: [ { test: /\.(js|jsx)$/, exclude: /node_modules/, use: {
module.exports = {
entry: [
'./src/index.js'
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use:
{
loader: 'babel-loader',
query: {presets: ['react', 'es2015']}
}
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
output: {
path: __dirname + '/dist',
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: "./src",
hot: true,
port: 3000,
historyApiFallback: true
} };
Index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React App Setup</title>
</head>
<body>
<div id='root'></div>
<script type="text/javascript" src="bundle.js"></script>
</body>
</html>
反应应用程序设置
和Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';
ReactDOM.render(<App />, document.getElementById('root'));
从“React”导入React;
从“react dom”导入react dom;
从“./components/App.jsx”导入应用程序;
ReactDOM.render(,document.getElementById('root'));
和App.js(如果需要)
import React from 'react';
export default class App extends React.Component {
render() {
console.log("made it here");
return (
<div style={{textAlign: 'center'}}>
<h1>Hello World</h1>
</div>);
}
}
从“React”导入React;
导出默认类App扩展React.Component{
render(){
console.log(“在这里制作”);
返回(
你好,世界
);
}
}
我可能使用了一些类型的加载器,但如果bundle.js没有加载,为什么我会在bundle.js中看到“Hello Word”和我的控制台日志语句呢?我无法从您提供的内容中看出您做错了什么,但我强烈建议通过npm安装和使用create react应用程序: 为此,可以在命令提示符/CLI中输入以下内容:
npm install -g create-react-app
create-react-app my-app
cd my-app
npm start
我把它用于我做过的每一个项目,在几秒钟内就能看到一个“hello world”。这将在目录
\my app
中的默认本地主机上运行hello world应用程序。。。将捆装在车身后面。:) 我不想要所有的沉重负担。这真的不是很好。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-谢谢@Davіd的指导-我已经做了相应的调整。请看下面的评论-如果您正在寻找v。轻量级您应该查看ParcelOh实际上,在html节点移动到html文档的
部分之前,您可能需要脚本标记。谢谢,这是关键,而不是头部,我在主体之后移动。