Home >Web Front-end >JS Tutorial >A brief discussion on how to import local JSON files in Angular
This article will introduce to you how to import local JSON files in Angular. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Angular supports Typescript2.9 starting from 6.1, with the help of Typescript’s new features, we can directly import local JSON files in any Typescript module by using import
. [Related recommendations: "angular Tutorial"]
To enable this new feature, you need to perform the following steps in Angular:
Create a JSON file anywhere in the project source code directory, for example:
src/assets/json/data.json
Set the following code under the compilerOptions option in the tsconfig.json
file:
{ ..., "compilerOptions": { ..., "resolveJsonModule": true, "esModuleInterop": true } }
Among them:
.json
suffix files.json
The file is requiredImport the JSON file in the component/command/service, the code is as follows:
// 你的JSON文件路径 import data from '../../assets/json/data.json';
uses Angular’s built-in httpCLient
service
Create a JSON file anywhere in the project source code directory, for example:
src/assets/json/data.json
httpClientModule module in the app.module.ts file, the code is as follows:
import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [..., HttpClientModule] }) export class AppModule {}
httpClient in the component/command/service to import the JSON file, the code is as follows:
import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-second-way', template: `<div>{{jsonDataResult | json}}` }) export class SecondWayComponent { jsonDataResult: any; constructor(private http: HttpClient) { this.http.get('assets/json/data.json').subscribe((res) => { this.jsonDataResult = res; console.log('--- result :: ', this.jsonDataResult); }); } }
src/assets/json/data.json
*.d.ts file in the directory where the json file is placed, for example:
We create data-typings.d.ts in the src/assets/json folder
Note: You can create this file in the src root directory, so that it can be declared globally; in addition, the file name is arbitrary, but the suffix must be .d.tsdeclare module '*.json' { const value: any; export default value; }
httpClient in the component/command/service to import the JSON file. The code is as follows:
// 你的JSON文件路径 import * as data from '../../assets/json/data.json'; // 或者像这样导入也可以 import data from '../../assets/json/data.json';
Notes: If it does not take effect and prompts an error message that further configuration of resolveJsonModule is required, then you need to check the
tsconfig.app.json configuration file in the project. There is an option: include, make sure it is in the configuration. * The path of
.d.ts, for example:
{ ..., "include": [ "src/**/*.d.ts" ] }For more programming-related knowledge, please visit:
Programming Video! !
The above is the detailed content of A brief discussion on how to import local JSON files in Angular. For more information, please follow other related articles on the PHP Chinese website!