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