英文:
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
<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 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:
<script src="appearance.js" type="module></script>
<script src="table-gen.js" type="module"></script>
The rest of the syntax seems fine. You can also use global variables like this:
// appearance.js
window.genModBtn = document.querySelector('.gen-mod-btn-container button');
// table-gen.js
console.log(genModBtn);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论