如何在没有node.js的情况下使用JavaScript导入/导出?
以下代码在使用VisualStudioCodeLive server运行时起作用。但在作为不同文件运行时引发错误。 我在我的网站上在线测试了它,并用完整的URL路径替换了“/course.js”和“/student.js”,返回的错误是:如何在没有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
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>
....