Angular 8和导入Json [英] Angular 8 and importing Json

查看:289
本文介绍了Angular 8和导入Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几篇文章,指出自从输入脚本2.9以来,您现在可以直接导入json. 因此,我对我的 tsconfig.json 文件进行了如下修改:

I have read a few articles stating you can now import json directly since type script 2.9. So I have amended my tsconfig.json file as follows:

{
  "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导入到我的组件中,如下所示:

And then I imported the json into my component like this:

import particlesJson from '../../assets/particles.json';

它运行正常,但在控制台中出现错误:

It runs fine, but I get an error in the console:

src/app/profile/login.component.ts(3,27)中的错误:错误TS2732:找不到模块'../../assets/particles.json'.考虑使用'--resolveJsonModule'导入扩展名为'.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,

根据文档,应将

放在tsconfig.json中的compilerOptions下. 参见示例此处.

should be placed under compilerOptions in tsconfig.json, as per documentation.
See the example here.

这篇关于Angular 8和导入Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆