gpt4 book ai didi

javascript - 无法在 module-javascript 外部使用 import 语句

转载 作者:行者123 更新时间:2023-11-28 16:56:55 25 4
gpt4 key购买 nike

我是一名自学学生,并且遇到“无法在 module-javascript 之外使用 import 语句”错误所以,我尝试从 hello.js 导入 hello() ..

///////////hello.js///////
export default function hello(){
console.log("hello")
}

////////////app.js/////////
import hello from "./hello"
hello()

最佳答案

importexport 需要将脚本类型设置为模块才能工作,除非您对其进行转译。试试这个:

<script type="module" src="app.js"></script> 

这相当于:

<script type="module">
import hello from "./hello"
hello()
</script>

关于javascript - 无法在 module-javascript 外部使用 import 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904330/

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