0

无论我把导出语句放在哪里,我都会得到同样的错误

导出声明只能出现在模块的顶层

我尝试将 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"
};

...
4

2 回答 2

0

导出内容的模块不应该有<script>标签。

导入这些东西的脚本需要一个<script type="module">标签来加载它。

它是import使浏览器请求模块的 URL 并加载它的语句,而不是单独的<script>标记。

于 2020-08-28T08:43:36.673 回答
0

我在包含 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>
于 2020-08-28T08:40:17.370 回答