找不到模块json

时间:2020-05-28 10:07:50

标签: json angular angular9 tsconfig

我需要导入assets filder中的这个json文件。

{
    "HOME": "home"
}

所以我要在组件中做

  import config from "../../../assets/file/config.json";

但我收到此错误:

Cannot find module '../../..//config.json'. Consider using '--resolveJsonModule' to import module with '.json' extension

所以我以这种方式修改了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",
    "lib": ["es2018", "dom"],
    "resolveJsonModule": true,
    "esModuleInterop": true
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

但是错误不会消失,抛出的错误相同。有人可以帮助我吗?

文件夹结构为

assets/ file/ and in the /file there is the config.jo;

2 个答案:

答案 0 :(得分:0)

请尝试以下导入语句:-

从“ ../../../assets/file/config.json”中导入*作为配置;

答案 1 :(得分:0)

尝试将资产直接导入为:

 import config from "assets/file/config.json";

此外,如果它不起作用,则可能需要在ts.config中设置“ resolveJsonModule”:true