我想在我的.ts文件中导入JSON文件。但是,当我执行require时,我收到运行时错误。
public serverConfiguration = require('./serverConfiguration.json');
这是我在.ts。
中添加JSON文件的方式在运行网页时,我收到以下错误:
Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/config/serverConfiguration.json.js
Error: XHR error (404 Not Found) loading http://localhost:3000/config/serverConfiguration.json.js
at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:698:29)
at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:265:35)
at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:154:47)
at XMLHttpRequest.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:335:33)
Error loading http://localhost:3000/config/serverConfiguration.json.js as "./serverConfiguration.json" from http://localhost:3000/config/config.js
at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:698:29)
at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:265:35)
at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:154:47)
at XMLHttpRequest.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:335:33)
Error loading http://localhost:3000/config/serverConfiguration.json.js as "./serverConfiguration.json" from http://localhost:3000/config/config.js
我已经在我的System.config.js中输入了一个条目,但仍然没有运气。
packages: {
serverConfiguration:{
defaultExtension : 'json'
},
我也试过导入JSON文件,但我认为,对于JSON文件也不能这样做。
先谢谢。
答案 0 :(得分:0)
您必须确保导出json:
var json = [{}]
exports.json = json;
现在您的要求将起作用:
var json = require('./serverConfiguration'); //<---change the extension to js