문서의 이전 판입니다!
A class with a specific purpose.
Naming convention - .service.ts
import { Injectable } from '@angular/core'; // @Injectable({ // providedIn: 'root' // }) @Injectable() export class EmployeeService { constructor() { } getEmployees(){ return [ { id: 3, name: "둘리", age: 100030 }, { id: 4, name: "고길동", age: 30 }, { id: 5, name: "희동이", age: 30 }, { id: 5, name: "고철수", age: 30 }, ]; } }
import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { IEmployee } from './employee'; //import { Observable } from 'rxjs'; import { throwError } from 'rxjs'; import { catchError, retry } from 'rxjs/operators'; // @Injectable({ // providedIn: 'root' // }) @Injectable() export class EmployeeService { private _url: string = "/assets/data/employees.json" constructor(private http: HttpClient) { } getEmployees(): Observable<IEmployee[]>{ return this.http.get<IEmployee[]>(this._url) .pipe( catchError(this.errorHandler) ); } errorHandler(error: HttpErrorResponse){ return throwError(error); } }