我正在尝试使用traceur。当我想加载模块时,我收到一条错误消息,表明加载不成功。我的代码基于其模块文档中提供的示例跟踪器。
这是 main.html:
<script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script>
<script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script>
<script>
System.traceurOptions = { experimental: true }
</script>
<script type="module" src="ProfileView.js"></script>
以及加载的模块:
// ProfileView.js
import {firstName, lastName, year} from './Profile.js';
function setHeader(element) {
element.textContent = firstName + ' ' + lastName;
}
// rest of module
<小时/>
// Profile.js
export var firstName = 'David';
export var lastName = 'Belle';
export var year = 1973;
我在 Chrome 中收到以下错误:
XMLHttpRequest cannot load file:///C:/Code/Tests/Traceur/ProfileView.js. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
WebPageTranscoder FAILED to load file:///C:/Code/Tests/Traceur/ProfileView.js
Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///C:/Code/Tests/Traceur/ProfileView.js'.
我知道你不能通过文件系统发出 xhr 请求,但我看过一些教程,其中代码的结构与我的类似,并且它可以在那里工作......
我可能做错了什么?
最佳答案
您无法使用 xhr 访问文件系统,您应该通过运行本地网络服务器通过 http 打开这些页面。如果您确实想启用文件系统访问,您可以:http://www.chrome-allow-file-access-from-file.com/
即你启动 chrome chrome.exe --allow-file-access-from-files
关于javascript - 无法使用 Traceur 加载模块 - 尝试通过 xhr 访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774814/