import { Injectable } from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import {LocalStorageService} from 'angular-2-local-storage'; @Injectable({ providedIn: 'root' }) export class ApiService { constructor( private http: HttpClient, private storage: LocalStorageService ) { } get(url) { return this.http.get(url, this.authOptions()); } private authOptions() { const auth = this.storage.get('basicAuth'); if (auth) { return {headers: new HttpHeaders({Authorization: 'Basic ' + auth})}; } else { return {}; } } }