【问题标题】:Javascript export/import doesn't workJavascript 导出/导入不起作用
【发布时间】:2019-01-11 19:11:42
【问题描述】:

我只想在另一个 js 文件中导入自定义 js 模块。

在 html 头上:

<script  src="Dictionary.js" type="module"></script>
<script type="text/javascript" src="Templates.js" ></script>

在模块上:

 var Dictionary=function() {...}
 export {Dictionary};

关于js文件:

import {Dictionary} from './Dictionary';

我收到这些错误

在 Chrome 上:

Uncaught SyntaxError: Unexpected token {

在火狐上

SyntaxError: import 声明只能出现在顶层 模块

这两个文件在同一个文件夹中。

【问题讨论】:

  • 尝试在第二个 &lt;script&gt; 标签上将 type="text/javascript" 更改为 type="module"
  • 当我这样做时,该文件从源文件中消失了。
  • 我再次尝试添加.js,它确实有效!非常感谢。
  • 你是怎么解决的?

标签: javascript


【解决方案1】:

导入时不要添加“.js”

import {Dictionary} from 'Dictionary';

请参阅文档here

【讨论】:

  • 没有解决问题仍然得到相同的错误。不过我已经更正了。
猜你喜欢
  • 1970-01-01
  • 2021-06-03
  • 1970-01-01
  • 2019-07-01
  • 2019-10-31
  • 2017-05-30
  • 2012-03-12
  • 2017-10-02
  • 1970-01-01
相关资源
最近更新 更多