简体   繁体   English

在 Angular 6 中导入 Json 文件?

[英]Import Json file in Angular 6?

Tried several different ways to import a json file but get尝试了几种不同的方法来导入 json 文件但得到

countries.json has unknown extension.

Is there a solution to this I'm missing?有没有解决我失踪的问题?

Current tsconfig:当前的 tsconfig:

  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,    
    "module": "es2015",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "types": [ "node"],
    
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  },
  "include": ["./src/**/*", "./src/**/*.json"],

}

Here the import is as follows import * as countries from './countries.json';这里的导入如下import * as countries from './countries.json';

And the setup of countries.json is this..和国家的设置。json 是这个..

   [
    {"name": "Afghanistan", "code": "AF"},
    {"name": "Åland Islands", "code": "AX"},
    ]

I believe Angular 6 json support is a bit wonky, so I would update the proyect to a newer version (which, with angular, is not a huge workload).我相信 Angular 6 json 支持有点不稳定,所以我会将项目更新到更新的版本(使用 angular,工作量不是很大)。 From Angular 7 onwards you can configure your tsconfig.json file like this:从 Angular 7 开始,您可以像这样配置 tsconfig.json 文件:

{
  "compileOnSave": false,
  "compilerOptions": {
    ...
    "resolveJsonModule": true,
    ...
}

This way you can import your.json files normally like this:这样你就可以像这样导入你的 .json 文件:

import  *  as  data  from  './data.json';

Hope this helps!希望这可以帮助!

Ill leave an article with an example an step by step: https://www.techiediaries.com/import-local-json-files-in-typescript/我会一步一步地留下一个例子: https://www.techiediaries.com/import-local-json-files-in-typescript/

import something from "somewhere" means that import the default exported value from path. import something from "somewhere"意味着从路径导入默认导出值。

Clearly, json files does not have any export as default.显然, json文件默认没有任何导出。 You can import everything or you can import some property from it.您可以导入所有内容,也可以从中导入一些属性。

To import everything from json file:要从 json 文件导入所有内容:

import * as something from "somewhere"

To import some property from json file: when the content of file is like从 json 文件导入一些属性:当文件的内容像

file.json文件.json

{
    "name": "Stackoverflow",
    "otherProperty": "otherValue"
}

To import only name :仅导入name

import { name } from "file.json"

But Typescript added --resolvejsonmodule option in the version 2.9 .但是Typescript2.9版本中添加了--resolvejsonmodule选项。 As I know Angular started to use Typescript 2.9 at the version 7 , so for your case:据我所知 Angular 在版本7开始使用Typescript 2.9 ,所以对于你的情况:

json.service.ts json.service.ts

@Injectable()
export class JsonService {
  constructor(private http: HttpClient) {}


  getJson(path: string) {
    return this.http.get(path);
  }
}

and do not forget to add your JsonService to providers array of your module and finally, you need to import HttpClientModule at your module.并且不要忘记将您的JsonService添加到模块的providers数组中,最后,您需要在模块中导入HttpClientModule

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM