如何解决导出时,获取导出声明只能出现在JavaScript中模块的顶层
无论我将export语句放在哪里,都会遇到相同的错误
导出声明只能出现在模块的顶层
我尝试将type =“ module”添加到脚本标签中,但没有帮助,我正尝试避免使用它。
JS :
export { LogTypes,DateTypes,GetTypes,get,show,hide } from ...;
const LogTypes = {
Default: "DEFAULT",Info: "INFO",Warn: "WARN",Error: "ERROR"
};
const DateTypes = {
Current: "CURRENT",Log: "LOG",Short: "SHORT",Long: "LONG"
};
const GetTypes = {
Name: "NAME",Id: "ID",Tag: "TAG",Query: "QUERY",QueryAll: "QUERYALL"
};
...
解决方法
当包含JavaScript文件并允许在CORS上进行连接时,我已经弄清楚了。
JS:
// log types
export const LogTypes = {
Default: "DEFAULT",Info: "INFO",Warn: "WARN",Error: "ERROR"
};
// date types
export const DateTypes = {
Current: "CURRENT",Log: "LOG",Short: "SHORT",Long: "LONG"
};
// supported types by the get function
export const GetTypes = {
Name: "NAME",Id: "ID",Tag: "TAG",Query: "QUERY",QueryAll: "QUERYALL"
};
// gets elements based on type and elm value
export const get = (type,elm) => {
let result = null;
switch (upper(type)) {
case GetTypes.Name:
result = document.getElementsByName(elm);
break;
case GetTypes.Id:
result = document.getElementById(elm);
break;
case GetTypes.Tag:
result = document.getElementsByTagName(elm);
break;
case GetTypes.Query:
result = document.querySelector(elm).elements;
break;
case GetTypes.QueryAll:
result = document.querySelectorAll(elm);
break;
default:
throw new Error(type + " not supported");
}
return result;
};
HTML:
<script src="/SCRIPT/Creator.js"></script>
,
正在导出内容的模块不应有<script>
标签。
导入这些脚本的脚本需要一个<script type="module">
标记才能加载。
这是import
语句,导致浏览器请求模块的URL并加载它,而不是单独的<script>
标记。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。