【发布时间】:2016-12-24 06:28:39
【问题描述】:
更新:问题已由this PR修复
请注意,问题出现并且普通纯 JavaScript 方法无法解决的原因可能是因为 server.ts 正在使用 TypeScript 和 网页包。检查甘特的答案。并在 github 上跟踪 this issue。
我使用angular/universal-starter 作为启动器。我有一个文件 config.json
{
"api": "123"
}
当我在 server.ts 中阅读 config 时:
import * as config from '../config.json';
// const config = require('../config.json'); will be same result
console.log(config);
它在终端中显示:
{
"api": "123"
}
但是,当我尝试在 server.ts 中读取 config.api 时:
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。
什么可能导致这种情况?谢谢
【问题讨论】:
-
import config from './config.json';。如果它在节点中,你可以只需要它。 const config = require('./config.json'). -
@SwarajGiri 然后
console.log(config);和console.log(config.api);将显示undefined -
我在该存储库中看不到任何名为
config.json的文件。 -
@SeeDart 我用它作为启动器,
config.json是我自己的文件。 -
您使用的是相对路径还是绝对路径?你能分享一下你的文件结构是什么样的吗?
标签: node.js angular typescript webpack angular2-universal