Я новичок в Angular и Http службы. Я создал список сотрудников и сервис с именем employee.service.ts, который делает вызов Http. Я также, правильно сопоставив наблюдаемое,
Я также создаю json файл в активах с именем сотрудника. json, в котором хранятся мои данные. Там нет никаких ошибок компиляции. но он терпит неудачу во время выполнения. Я получаю следующую ошибку.
NullInjectorError: R3InjectorError(AppModule)[EmployeeService -> EmployeeService -> EmployeeService]:
employee-list.component. html
<h2>Employee List</h2>
<ul *ngFor="let employee of employees">
<li>{{employee.name}}</li>
</ul>
employee.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IEmployee } from '../employee';
import { Observable } from 'rxjs';
@Injectable() export class EmployeeService{
private _url: string ="/assets/data/employee.json";
constructor(private http:HttpClient) {}
getEmployees(): Observable<IEmployee[]>{
return this.http.get<IEmployee[]>(this._url);
}
}
employee-list.component.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { IEmployee } from '../employee';
import { Observable } from 'rxjs';
@Injectable() export class EmployeeService{
private _url: string ="/assets/data/employee.json";
constructor(private http:HttpClient) {}
getEmployees(): Observable<IEmployee[]>{
return this.http.get<IEmployee[]>(this._url);
}
}