How to fix babel react "Uncaught SyntaxError: Unexpected token <"
How to fix babel react "Uncaught SyntaxError: Unexpected token <"
我在通过 CDN 设置 React 时遇到问题。
她是我的html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
这是我的 app.js:
var myItems = {
items:
["Widget","Gear","Sprocket","Spring","Crank","Lever","Hose","Tube",
"Wheel","Housing","Case"]
}
var FilteredList = React.createClass({
filterList: function (event) {
var updatedList = this.props.initialItems.items;
updatedList = updatedList.filter(function (item) {
return item.toLowerCase().search(event.target.value.toLowerCase()) !== 1;
});
this.setState({items: updatedList});
},
getInitialState: function(){
return {
items: []
}
},
componentWillMount: function(){
this.setState({items: this.props.initialItems.items})
},
render: function(){
return (
<div className="myList">
<input type="text" placeholder="Search" onChange={this.filterList} />
<List items={this.state.items}/>
</div>
);
}
});
var List = React.createClass({
render: function(){
return (
<ul>
{
this.props.items.map(function (item) {
return <li key={item}>{item}</li>;
})
}
</ul>
);
}
});
ReactDOM.render(<FilteredList initialItems={myItems} />,
document.getElementById('container'));
当 运行 时,我得到以下错误:
app.js:31 Uncaught SyntaxError: Unexpected token <
知道如何解决吗?
即使您已将 babel-standalone 添加到您的页面,您仍然需要告诉它转译您的 app.js 文件。
您可以通过将 type="text/babel"
添加到您的脚本标签来实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"> </script>
<script type="text/babel" src="js/app.js"></script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
仅供参考,您还可以内联转换代码:
Babel.transform(input, { presets: ['es2015'] }).code;
我在通过 CDN 设置 React 时遇到问题。
她是我的html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"></script>
<script src="js/app.js"></script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
这是我的 app.js:
var myItems = {
items:
["Widget","Gear","Sprocket","Spring","Crank","Lever","Hose","Tube",
"Wheel","Housing","Case"]
}
var FilteredList = React.createClass({
filterList: function (event) {
var updatedList = this.props.initialItems.items;
updatedList = updatedList.filter(function (item) {
return item.toLowerCase().search(event.target.value.toLowerCase()) !== 1;
});
this.setState({items: updatedList});
},
getInitialState: function(){
return {
items: []
}
},
componentWillMount: function(){
this.setState({items: this.props.initialItems.items})
},
render: function(){
return (
<div className="myList">
<input type="text" placeholder="Search" onChange={this.filterList} />
<List items={this.state.items}/>
</div>
);
}
});
var List = React.createClass({
render: function(){
return (
<ul>
{
this.props.items.map(function (item) {
return <li key={item}>{item}</li>;
})
}
</ul>
);
}
});
ReactDOM.render(<FilteredList initialItems={myItems} />,
document.getElementById('container'));
当 运行 时,我得到以下错误:
app.js:31 Uncaught SyntaxError: Unexpected token <
知道如何解决吗?
即使您已将 babel-standalone 添加到您的页面,您仍然需要告诉它转译您的 app.js 文件。
您可以通过将 type="text/babel"
添加到您的脚本标签来实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="css/main.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.7.7/babel.min.js"> </script>
<script type="text/babel" src="js/app.js"></script>
</head>
<body>
<div id="container">
</div>
</body>
</html>
仅供参考,您还可以内联转换代码:
Babel.transform(input, { presets: ['es2015'] }).code;