gpt4 book ai didi

javascript - ReactJS:意外的 token <

转载 作者:行者123 更新时间:2023-11-28 15:12:29 25 4
gpt4 key购买 nike

TL;DR:为什么我的代码会出现错误? Previous question是不同的(在线与桌面),它的答案对我不起作用。

完整代码here

基于或多或少源自 here 的代码(我的“类(class)”还没有结束

问题:关注this “ReactJS 简介”。本演练有 Webpack/Babel 设置。它使用纯 JS 运行,但是当我切换到 JSX 时,它就卡住了。这类似于 this question ,但这些答案似乎都不起作用。主要区别:网络 Playground 与我的盒子上的本地 Playground ?

我正在处理的视频的结尾指向 this code - 不过,我只完成了 3/4,所以部分内容尚未包含在内。所以,我又拨回 this fork with my edits (抱歉,如果我已经放弃了 fork 和推送我的更改......)

注释: BEFORE 和 AFTER 是我唯一更改的内容。它适用于 javascript/jquery - 但不适用于 JSX。我发现了一些拼写错误、大小写错误(thisItem 与 thisitem)以及一些不应该出现的项目(删除了括号)。

我已经更改了“我的”键入的版本,以更紧密地匹配“他们的”版本(Hello 而不是 HelloWorld),并进行了其他细微更改...相同的错误。

除了一些间距问题之外,我看到的最大的剩余变化是版本 - 记录类的次要版本碰撞。

我的代码:

.babelrc

{ "presets": [ "react" ] }

package.json

 {
"name": "github-battle",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server",
"production": "webpack -p"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^0.14.7",
"react-dom": "^0.14.7"
},
"devDependencies": {
"babel-core": "^6.6.0",
"babel-loader": "^6.2.4",
"babel-preset-react": "^6.5.0",
"html-webpack-plugin": "^2.9.0",
"webpack": "^1.12.14",
"webpack-dev-server": "^1.14.1"
}
}

webpack.config.js

var HtmlWebpackPlugin = require('html-webpack-plugin')
var HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
template: __dirname + '/app/index.html',
filename: 'index.html',
inject: 'body'
})

module.exports = {
entry: [
'./app/index.js'
],
output: {
path: __dirname + '/dist',
filename: "index_bundle.js"
},
module: {
loaders:[
{ test: /\.js$/,include: __dirname + '/app',loader: "babel-loader" }
]
},
plugins: [HtmlWebpackPluginConfig]
}

应用程序\index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Github Battle</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
</head>
<body>
<div id="app"></div>
</body>
</html>

app\index.js 之前

var app = document.getElementbyid('app')
app.innerHTML = 'Hello

app\index.js 之后

var React = require('react');
var ReactDOM = require('react-dom');

var HelloWorld = React.createClass({
render: function () {
return <div> Hello World </div>
}
});

ReactDOM.render(
<HelloWorld />,
document.getElementById('app')
);

结果:

> B:\Users\Chris\react-js\React-Fundamentals>npm run production
> gh-battle@1.0.0 production B:\Users\Chris\react-js\React-Fundamentals
> webpack -p

Hash: 21e367e251c35209471c
Version: webpack 1.12.14
Time: 375ms
Asset Size Chunks Chunk Names
index_bundle.js 289 bytes 0 [emitted] main
index.html 305 bytes [emitted]
[0] multi main 28 bytes {0} [built] [1 error]
[1] ./app/index.js 0 bytes [built] [failed]

ERROR in ./app/index.js
Module parse failed: B:\Users\Chris\react-js\React-Fundamentals\app\index.js Line 6: Unexpected token <
You may need an appropriate loader to handle this file type.
| var Hello = React.createClass({
| render: function () {
| return <div> Hello ReactJS World! </div>
| }
| });
@ multi main
Child html-webpack-plugin for "index.html":
+ 3 hidden modules

B:\Users\Chris\react-js\React-Fundamentals>

webpack.config.js #2:同样的错误

var HtmlWebpackPlugin = require('html-webpack-plugin');
...
module.exports = {
...
module: {
loaders: [
{test: /\.js$/, include: __dirname + '/app', loader: "babel-loader"}
],
query: {
presets: ['react']
}
},
plugins: [HTMLWebpackPluginConfig]
};

最佳答案

您需要将 index.js 重命名为 index.jsx

关于javascript - ReactJS:意外的 token <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756241/

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