在我的 Angular 项目中,我正在为我自己的小型本地化服务导入 JSON 文件。我正在使用 the method suggested here ,将我的 typings.d.ts
更新为
declare module "*.json" {
const value: any;
export default value;
}
这在 Angular 6 上运行良好,但在更新到 Angular 7 之后,当我尝试访问属性时,我的导入似乎未定义。
import * as de from './strings/de.json';
import * as en from './strings/en.json';
var s = en["mykey"]
JSON 有一个非常简单的键 => 值结构:
{
"myKey": "My Headline",
…
}
6.1 和 7 之间发生了什么变化可能会导致这种行为?
最佳答案
结果是 Angular 7 和 TypeScript 3.1 there actually have been some changes(我猜他们从 TS 2.9+ 开始就有了?)。使用问题中的代码,所有值都包含在“默认”对象中。为了防止这种情况,我不得不简化导入语句:
import de from './strings/de.json';
import en from './strings/en.json';
关于如何在 TypeScript 中导入 JSON 文件的 Also see this question for more details。
关于json - 在 Angular 7 项目中导入 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52888238/