gpt4 book ai didi

javascript - Webpack/Babel ES6 错误 : Uncaught SyntaxError: Unexpected token import

转载 作者:行者123 更新时间:2023-11-30 09:44:25 25 4
gpt4 key购买 nike

我是 ES6 的新手,如果这是一个重复的问题,请原谅我。如标题所述,我目前收到错误:

Uncaught SyntaxError: Unexpected token import

显然,对象 Person 尚未成功导入 app.js 文件中使用。

我查看了网络上的示例代码,但仍然无法正常工作。我怀疑它与 webpack.config.js 设置有关。

我也尝试过使用 let person = require("./modules/Person"); 但它不起作用。

请问有什么想法吗?他们将不胜感激。

我在下面提供了代码摘录:

src/js/modules/person.js

class Person {

constructor(name, jobtitle, sex){
this.name = name;
this.jobtitle = jobtitle;
this.message = document.getElementById("message").html;
this.sex = sex;
}

Greet(){
console.log("Hello, my name is "+ this.name + " and i am a " + sex);
}

EchoOccupation(){
console.log("I am a "+ this.jobtitle);
}

EchoMessage(){
console.log("The message is "+ this.message);
}
}

module.exports = Person;

src/js/app.js

import { Person } from './modules/person';

let vic = new Person("Fred", "Web Developer", "male");
vic.Greet();
vic.EchoOccupation("Web Developer");

webpack.config.js

var path = require("path"),
watchBabel = require("watch-babel"),
srcJsDir = "./src/js/",
srcDestJsDir = "dist/assets/js/",
options = { glob: "src/*.js" },
watcher = watchBabel(srcJsDir, srcDestJsDir, options);

watcher.on("ready", function() {
console.log("ready");
}).on("success", function(filepath) {
console.log("Transpiled ", filepath);
}).on("failure", function(filepath, e) {
console.log("Failed to transpile", filepath, "(Error: ", e);
}).on("delete", function(filepath) {
console.log("Deleted file", filepath);
});

module.exports = {

entry: [srcJsDir + "/app.js"],
output: {
path: srcDestJsDir,
filename: "app.js"
},
watch: true,
devServer: {
inline: true,
port: 8000
},
module: {
loader : [
{
test: /\.js$/,
exclude: "/node_modules/",
loaders: ['babel', 'watch-babel'],
query: {
presets: ['es2015'],
plugins: ['babel-plugin-uglify']
}
},
]
},
resolveLoader: {
root: path.join(__dirname, 'node_modules/')
}
};

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>im loving es6</title>
<link href="/src/css/frameworks/bootstrap.min.css" />
</head>
<body>
<header></header>
<main>
<div class="container">
<div class="row">
<div class="col-xs-12">
<article>
<section>
<div id="app"></div>
<div id="message">Hello this is my message!</div>
</section>
<aside></aside>
</article>
</div>
</div>
</div>
</main>
<footer></footer>
<script type="text/javascript" src="/dist/assets/js/app.js"></script>
</body>
</html>

最佳答案

错误发生是因为从person.js导出的模块没有Person属性。

您可以通过进行三项更改来解决此问题:

src/js/modules/person.js

class Person {
...
}

// Set the Person class as the object exported from this module
export default Person;

src/js/app.js

// Import the Person class
import Person from './modules/person';

webpack.config.js

...
module: {
// Rename the 'loader' property to 'loaders'
loaders: [
...
]
}

您可以在此处找到有关import 的更多信息:ES6 import

关于javascript - Webpack/Babel ES6 错误 : Uncaught SyntaxError: Unexpected token import,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520444/

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