2017-04-10 54 views
0

我想在我的一個.ts文件中導入JSON文件。但是,當我做一個require.I時,我得到一個運行時錯誤。在打字稿中導入JSON時出現問題

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

@詹姆斯·唐納利....你能爲類似提供鏈接Question.I嘗試過,但沒有發現任何類似的東西。 – user3856563

回答

0

你必須確保你導出JSON:

var json = [{}] 
exports.json = json; 

現在,您需要將工作:

var json = require('./serverConfiguration'); //<---change the extension to js 
+0

@Jai ....我應該進行更改並在我的.ts中添加上面的代碼片段,以便在其中導入json或其他文件? – user3856563