我想使用从另一个 (generateObject.js) 文件导入的 html onload 事件和 console.log 文本调用我的函数 main(),但是当我导入函数时,onload 事件停止工作并且不再使用函数 main()。
html:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="main.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body onload="main()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
生成对象.js:
export function hello() {
return "Hello";
}
Run Code Online (Sandbox Code Playgroud)
主要.js:
import { hello } from './generateObject.js';
function main(){
console.log(hello());
}
main();
Run Code Online (Sandbox Code Playgroud)
当我在 main() 中尝试 console.log("text") 时它可以工作,但是当我尝试使用导入的函数时它不是。我该怎么做才能解决这个问题?
Chrome 控制台中的错误:
未捕获的语法错误:不能在模块外使用导入语句 (main.js:1)
index.html:8 Uncaught ReferenceError: main 未在加载时定义 (index.html:8)