UPDATE:這個問題是由this PR無法讀取config.json正確(打字稿,的WebPack)
固定的是,之所以潔具,問題出來和正常純JavaScript的方式不能解決這可能是因爲server.ts正在使用TypeScript和Webpack。檢查Gant的答案。並在github上跟蹤this issue。
我使用angular/universal-starter作爲首發。我有一個文件config.json
{
"api": "123"
}
當我讀到config
在server.ts:
import * as config from '../config.json';
// const config = require('../config.json'); will be same result
console.log(config);
它顯示這個終端:
{
"api": "123"
}
然而,當我嘗試讀取config.api
server.ts:
import * as config from '../config.json';
// const config = require('../config.json'); will be same result
console.log(config.api);
它顯示undefined
。
這是我的文件夾結構(其他部分與angular/universal-starter相同)。
my-app
- config.json
+ src
- server.ts
而當我啓動應用程序時,我使用npm start
。
什麼可能導致這種情況?由於
'從 './config.json' 進口配置;'。如果它在節點中,則可以只需要它。 const config = require('./ config.json')。 –
@SwarajGiri然後'console.log(config);'和'console.log(config.api);'會顯示'未定義' –
我沒有在該資源庫中看到任何名爲'config.json'的文件。 – Skam