Create a service

The next step is to create a service that interacts with the API that we created earlier:

ng generate service user

This creates a user service called the user.service.ts. Next, import UserService class into app.module.ts and include it to the providers array:

Import rxjs/add/operator/map in the imports section.import { Injectable } from '@angular/core';import { Http, Headers } from '@angular/http';import 'rxjs/add/operator/map';Within the UserService class, define a constructor and pass in the angular 2 HTTP service.import { Injectable } from '@angular/core';import { Http, Headers } from '@angular/http';import 'rxjs/add/operator/map';@Injectable()export class UserService {constructor(private http: Http) {}}

Within the service ...

Get TypeScript 2.x for Angular Developers now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.