【发布时间】:2019-12-25 17:17:41
【问题描述】:
我已经阅读了一些文章,说明您现在可以直接导入 json,因为类型脚本 2.9。 所以我修改了我的 tsconfig.json 文件如下:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
},
"paths": {
"@data/*": ["src/core/data/*"],
"@models/*": ["src/core/models/*"],
"@services/*": ["src/core/*"],
"@environments/*": ["src/environments/*"]
},
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
}
然后我像这样将 json 导入到我的组件中:
import particlesJson from '../../assets/particles.json';
它运行良好,但我在控制台中收到错误:
src/app/profile/login.component.ts(3,27) 中的错误:错误 TS2732:找不到模块“../../assets/particles.json”。考虑使用“--resolveJsonModule”导入扩展名为“.json”的模块
如何阻止该错误出现?
【问题讨论】:
-
您使用的打字稿版本是什么?
标签: json angular typescript