为什么模块显示已连接但仍无法正常工作?

Why a module is shown connected but still not working?

每当我连接一个 JS 模块时,在 DevTools 的“Sources”中,该模块显示为已连接。 但是,我尝试 运行 的功能根本不起作用。

运行模块中的函数可以做什么?

function consoleLog () {
    console.log('The module is working')
}

export default consoleLog;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="module" src="./module.js">
        import consoleLog from './module';
        consoleLog();
    </script>
</head>
<body>

</body>
</html>

你的问题是你的脚本标签上有代码和 src 属性,脚本标签应该有一个或另一个。如果您将模块导入另一个模块,您也不必为模块创建脚本标签,如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="module">
        import consoleLog from './module.js';
        consoleLog();
    </script>
</head>
<body>

</body>
</html>