6: Get Data from a Server
[examples/angular-tour-of-heroes] / src / app / hero.service.ts
index 6df713d..349d043 100644 (file)
@@ -31,6 +31,14 @@ export class HeroService {
       );
   }
 
+  /** POST: add a new hero to the server */
+  addHero(hero: Hero): Observable<Hero> {
+    return this.http.post<Hero>(this.heroesUrl, hero, this.httpOptions).pipe(
+      tap((newHero: Hero) => this.log(`added hero w/ id=${newHero.id}`)),
+      catchError(this.handleError<Hero>('addHero'))
+    );
+  }
+
   /** GET hero by id. Will 404 if id not found */
   getHero(id: number): Observable<Hero> {
     this.log(`requested hero id=${id}`);