Uncaught SyntaxError: Unexpected token 'export', but module 'import' still works

huangapple go评论67阅读模式
英文:

Uncaught SyntaxError: Unexpected token 'export', but module 'import' still works

问题

I'm confused here. I'm a newbie.

index.html

  <script src="appearance.js"></script>
  <script src="table-gen.js" type="module"></script>

appearance.js

  export const genModBtn = document.querySelector('.gen-mod-btn-container button');

table-gen.js

  import { genModBtn } from "./appearance.js";
  console.log(genModBtn);

Why am I getting this 'Unexpected token' if all seems to be working fine?

The import { genModBtn } from "./appearance.js"; seems to be working fine since console.log(genModBtn) is working too.

I've tried to remove export from export const genModBtn = document.querySelector('.gen-mod-btn-container button'), but without it, all stops working and it still logs a different error.

What am I doing wrong here? Thank you in advance!

英文:

I'm confused here. I'm a newbie.

index.html

  &lt;script src=&quot;appearance.js&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;table-gen.js&quot; type=&quot;module&quot;&gt;&lt;/script&gt;

appearance.js

  export const genModBtn = document.querySelector(&#39;.gen-mod-btn-container button&#39;);

table-gen.js

  import { genModBtn } from &quot;./appearance.js&quot;;
  console.log(genModBtn);

Why am I getting this 'Unexpected token' if all seems to be working fine?

The import { genModBtn } from &quot;./appearance.js&quot;; seems to be working fine since console.log(genModBtn) is working too.

I've tried to remove export from export const genModBtn = document.querySelector(&#39;.gen-mod-btn-container button&#39;), but without it, all stops working and it still log a different error.

What am I doing wrong here? Thank you in advance!

答案1

得分: -1

你应该将它们都标记为模块:

<script src="appearance.js" type="module"></script>
<script src="table-gen.js" type="module"></script>

其余的语法看起来没问题。你也可以像这样使用全局变量:

// appearance.js
window.genModBtn = document.querySelector('.gen-mod-btn-container button');

// table-gen.js
console.log(genModBtn);
英文:

You should mark both of them as modules:

&lt;script src=&quot;appearance.js&quot; type=&quot;module&gt;&lt;/script&gt;
&lt;script src=&quot;table-gen.js&quot; type=&quot;module&quot;&gt;&lt;/script&gt;

The rest of the syntax seems fine. You can also use global variables like this:

// appearance.js
window.genModBtn = document.querySelector(&#39;.gen-mod-btn-container button&#39;);

// table-gen.js
console.log(genModBtn);

huangapple
  • 本文由 发表于 2023年6月12日 00:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定