3 回答

TA貢獻1799條經(jīng)驗 獲得超6個贊
模塊將有自己的范圍。它們不像普通腳本那樣在全局范圍內(nèi)可用。所以它只能main.js在你的情況下訪問。
要使其工作,您需要將其顯式添加到全局范圍。
import { hello } from './generateObject.js';
function main(){
console.log(hello());
}
window.main = main;
或者,您可以從 HTML 中刪除事件處理程序并將其添加到 JS 文件中。
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>
</body>
</html>
main.js
import { hello } from './generateObject.js';
function main(){
console.log(hello());
}
window.addEventListener('load', main)

TA貢獻1818條經(jīng)驗 獲得超7個贊
生成對象.js:
export function hello() {
return "Hello";
}
主.js:
import { hello } from './generateObject.js';
function main(){
console.log(hello());
}
main();
添加回答
舉報