我开始学习ReactJS,并且按照入门指南中的说明进行操作。我的目录结构如下所示:
app/App.js
node_modules
index.html
package.json
webpack.config.js
我认为问题的根源是来自CLI的以下错误消息:
ERROR in ./app/App.js
Module build failed: SyntaxError: c:/code/pro-react/my-app/app/App.js: Unexpected token (6:6)
4 | render() {
5 | return (
> 6 | <h1>Hello World</h1>
| ^
7 | );
8 | }
9 | }
的内容App.js
是:
import React from 'react';
class Hello extends React.Component {
render() {
return (
<h1>Hello World</h1>
);
}
}
React.render(<Hello />, document.getElementById('root'));
以下是内容package.json
:
{
"name": "my-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node_modules/.bin/webpack-dev-server --progress",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.4.5",
"babel-loader": "^6.2.1",
"webpack": "^1.12.11",
"webpack-dev-server": "^1.14.1"
},
"dependencies": {
"react": "^0.14.6"
}
}
和的内容webpack.config.js
是:
module.exports = {
entry: __dirname + "/app/App.js",
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [{
test: /\.jsx?$/,
loader: 'babel'
}]
}
};
我使用以下命令从CLI启动应用程序:
npm start
当我http://localhost:8080
在开发工具中访问时,出现错误消息:
GET http://localhost:8080/bundle.js 404 (Not Found)
但是,正如我所说,我认为罪魁祸首是它不喜欢语法,因此无法创建bundle.js
文件。请让我知道我在做什么错。
我想这是因为使用的是babel-6
没有babel presets
,在这种情况下,你需要babel-preset-es2015
和babel-preset-react
,
# For ES6/ES2015 support
npm install babel-preset-es2015 --save-dev
# Fot JSX support
npm install babel-preset-react --save-dev
然后更改webpack
配置
{
test: /\.jsx?$/,
loader: 'babel',
query: {
presets: ['es2015', 'react'],
}
}
或代替使用,query
您可以创建.babelrc
包含内容的文件
{
"presets": ["es2015", "react"]
}
您还需要安装react-dom
并使用ReactDOM.render
insta或React.render
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句