无论我把导出语句放在哪里,我都会得到相同的错误
导出声明只能出现在模块的顶层
我尝试添加type="模块";到脚本标签,但它没有帮助,我正在努力避免它。
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"
};
...
导出内容的模块不应该有<script>
标记。
导入这些东西的脚本需要一个<script type="module">
标签来加载它。
是import
语句导致浏览器请求模块的URL并加载它,而不是单独的<script>
标记。
我在包含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>