我读过几篇文章说自从 type script 2.9 版本之后,现在可以直接导入 JSON 格式文件了。因此,我已经修改了我的 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';
程序运行正常,但控制台中出现了一个错误:
ERROR in src/app/profile/login.component.ts(3,27): error TS2732: 找不到模块'../../assets/particles.json'。请考虑使用“--resolveJsonModule”导入带有“.json”扩展名的模块
我该如何避免这个错误的出现?