gpt4 book ai didi

json - Angular 8和导入Json

转载 作者:行者123 更新时间:2023-12-03 14:46:05 24 4
gpt4 key购买 nike

我已经阅读了几篇文章,指出自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: Cannot find module '../../assets/particles.json'. Consider using '--resolveJsonModule' to import module with '.json' extension



如何阻止该错误出现?

最佳答案

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

根据文档,应将其放在 compilerOptions中的 tsconfig.json下。
参见示例 here

关于json - Angular 8和导入Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57574481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com