首页 > 解决方案 > 角度 7 - 导入 json 文件 - 编译错误

问题描述

angular 7 的一个非常好的特性是对 typescript 3.1 的支持:

https://alligator.io/angular/angular-7/

我在 tsconfig.json 文件中添加了以下 3 行 - 在“compilerOptions”部分下:

"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true

一切看起来都不错——我可以毫无问题地遍历我的 json 文件。

但由于某种原因,我的 IDE 中出现“TS2307:找不到模块”错误:

在此处输入图像描述

我尝试将导入语法更改为此处的建议: Importing json file in TypeScript

但没有成功。

有任何想法吗?

编辑:我使用Webstorm作为 IDE。

标签: angulartypescriptcompiler-errors

解决方案


所以你没有提到你正在使用的 IDE,但我使用 VSCode,帮助我添加的是:

"angularCompilerOptions": {
"annotateForClosureCompiler": false  
}

src/tsconfig.app.json

我不再有导入问题,我的 IDE 带有.json文件。专业提示,尝试:

import { default as awsData } from `....`

这将摆脱作为default属性添加到返回的 JSON 对象中的导入。


推荐阅读