Javascript 不能在模块外使用 import 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58357941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:20:23 来源:igfitidea点击:
Cannot use import statement outside a module
提问by Walter Dudley
I'm trying to use classes in pure JavaScript, so I'm facing the error "Uncaught SyntaxError: Cannot use import statement outside a module" and can't solve it.
我试图在纯 JavaScript 中使用类,所以我面临错误“未捕获的语法错误:无法在模块外使用导入语句”并且无法解决它。
File1.js - Main file
File1.js - 主文件
import example from "./file2";
var test = new example();
File2.js - Class file
File2.js - 类文件
export default class example {
constructor() {
console.log("hello world");
}
}
回答by marzelin
add files with type="module":
添加文件type="module":
<script src="file1.js" type="module"></script>

