一点浩然气,千里快哉风。——宋•苏轼
昨天公司实习生问我这个import
为什么一直报错,他代码如下:
报错信息如下:
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
在外部引用
<script type="module"> import ruben from './js/module.js' console.log(ruben) </script>