User service

Alright, we've prepared our model and database for handling users within our application. Now, we can create a new service that we are using in our container components to obtain the currently logged in user. Let's create a user service using the Angular CLI:

ng generate service --spec false user/user

Let's open the stub service generated on the  src/app/user/user.service.ts path and replace its content with the following code:

import {Injectable} from '@angular/core';import {HttpClient} from '@angular/common/http';import {User} from '../model';@Injectable()export class UserService {  constructor(private http: HttpClient) {  }  getCurrentUser() {    return this.http.get<User>('/api/users/1');  }}

We're keeping things simple here. The ...

Get Mastering Angular Components now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.