Uncaught SyntaxError: Cannot use import statement outside a module
HTML 网页中,浏览器通过 script 标签加载 JavaScript 脚本。由于浏览器脚本的默认语言是 JavaScript,因此type="application/javascript"可以省略。在报错中了解到,是说无法在模块外部使用import语句,因为Module 的加载实现的是es6语法,所以在浏览器加载html文件时,需要在script 标签中加入type="module"属性。
Reference:
module
: For HTML5-compliant browsers the code is treated as a JavaScript module. The processing of the script contents is not affected by thecharset
anddefer
attributes. For information on usingmodule
, see ES6 in Depth: Modules. Code may behave differently when themodule
keyword is used.