Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有node.js的情况下使用JavaScript导入/导出?_Javascript_Html - Fatal编程技术网

如何在没有node.js的情况下使用JavaScript导入/导出?

如何在没有node.js的情况下使用JavaScript导入/导出?,javascript,html,Javascript,Html,以下代码在使用VisualStudioCodeLive server运行时起作用。但在作为不同文件运行时引发错误。 我在我的网站上在线测试了它,并用完整的URL路径替换了“/course.js”和“/student.js”,返回的错误是: Cross-Origin Request Blocked: The Same Origin Policy disallows readin the remote resource at .... (Reason: CORS header 'Access-Con

以下代码在使用VisualStudioCodeLive server运行时起作用。但在作为不同文件运行时引发错误。 我在我的网站上在线测试了它,并用完整的URL路径替换了“/course.js”和“/student.js”,返回的错误是:

Cross-Origin Request Blocked: The Same Origin Policy disallows readin the remote resource at .... (Reason: CORS header 'Access-Control-Allow-Origin' missing).

在另一次迭代中,我使用che将请求头设置为允许CORS,但由于浏览器返回以下错误,我仍然无法正确运行代码:

  SyntaxError: export declarations may only appear at top level of a module
请记住,我没有使用node.js,也没有使用任何形式的服务器端渲染,而是尝试导入代码,而不必为每个js文件添加代码

范例

文件1(course.js)

文件2(student.js)

文件3(index.html)


  SyntaxError: export declarations may only appear at top level of a module
export function Course() {
   this.name = '';
}
import { Course } from './course.js';

export function Student() {
   this.course = new Course();
}
<html ...
<body>
    <script type='module'>
        import { Student } from './student.js';

        var x = new Student();
        x.course.name = 'xxx';
        console.log(x.course.name)
   </script>
 ....