昨天公司实习生问我这个import为什么一直报错,他代码如下:


报错信息如下:
代码语言:javascript复制Uncaught SyntaxError: Cannot use import statement outside a module
实际上是因为script标签没有加type导致的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<script type="module">
import {ruben} from './js/module.js'
console.log(ruben)
</script>
</body>
</html>
这样就可以了
可以看到正常输出

当然,我们使用export default也是一样的
let ruben = "module"
export default ruben在外部引用
代码语言:javascript复制<script type="module">
import ruben from './js/module.js'
console.log(ruben)
</script>


