Reputation: 456
I am trying to read a JSON file in an Angular 8 library which I am creating. I want to read the JSON file to load translations and implement internationalization for one of the component in the library.
Additional information: The JSON file is present in the src folder only, and I want to read its content and load translations in my service.
Upvotes: 2
Views: 6556
Reputation: 17610
Demo write one service to read json and you can use httpclient to reach json
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class JsonService {
constructor(private http: HttpClient) { }
getData(url): Observable<any> {
return this.http.get<any>(url);
}
}
then in component take from service
constructor(private json: JsonService ) {
json.getData('/url').subscribe((result)=> {
console.log(result)
});
}
As a second way you can open "resolveJsonModule": true
in "compilerOptions"
and call in component like
import * as employeeData from "./test.json";
Upvotes: 4