As @Yury explained, you need to get the subscription to the component level from the service level.
To make it even better what you can do is,
Create an API service like the following:
api.service.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { map, catchError, timeout, retry } from 'rxjs/operators';
import { CookieService } from 'ngx-cookie';
import { SERVER } from './static/static.json';
@Injectable({
providedIn: 'root'
})
export class ApiService {
user: Observable<any>;
headers: HttpHeaders;
constructor(
private http: HttpClient,
private cookieService: CookieService
) { }
get(url: string, options?: any) {
let key = SERVER.api + url;
this.headers = new HttpHeaders();
if(typeof options == 'string' && options != " "){
this.headers = new HttpHeaders().append('Authorization', options);
}
return this.http.get(key, {headers: this.headers, withCredentials: false})
.pipe(
timeout(15000),
retry(5),
catchError(err => throwError(err))
)
}
post(url: string, data: any, options?: any) {
let key = SERVER.api + url;
this.headers= new HttpHeaders();
if(typeof options == 'string' && options != " "){
this.headers = new HttpHeaders().append('Authorization', options);
}
return this.http.post<any>(key , data, {headers: this.headers, withCredentials: false})
.pipe(
catchError(err => throwError(err))
);
}
put(url: string, data: any, options?: any) {
let key = SERVER.api + url;
this.headers= new HttpHeaders();
if(typeof options == 'string' && options != " "){
this.headers = new HttpHeaders().append('Authorization', options);
}
return this.http.put<any>(key , data, {headers: this.headers, withCredentials: false})
.pipe(
catchError(err => throwError(err))
);
}
delete(url: string, options?: any) {
let key = SERVER.api + url;
this.headers= new HttpHeaders();
if(typeof options == 'string' && options != " "){
this.headers = new HttpHeaders().append('Authorization', options);
}
return this.http.delete<any>(key, {headers: this.headers, withCredentials: false})
.pipe(
catchError(err => throwError(err))
);
}
}
Then create a Module level service,
abs.service.ts
import { Injectable } from '@angular/core';
import { ApiService } from './shared/api.service';
import { AuthService } from './shared/auth.service';
@Injectable({
providedIn: 'root'
})
export class ABSService {
constructor(
private _API: ApiService,
private _AUTH: AuthService
){}
getUsers(option?) {
let url = '/users';
let token;
if(option){
url = url + "?" + option;
}
if(this._AUTH.loginCheck()){
token = this._AUTH.getCookie();
}
return this._API.get(url, token);
}
postUsers(data) {
let url = '/users';
let token;
if(this._AUTH.loginCheck()){
token = this._AUTH.getCookie();
}
return this._API.post(url,data, token);
}
}
Then you can use this module-level service in the module level component like this:
abs.component.ts
import { Component, OnInit } from '@angular/core';
import { ABSService } from './abs.service';
@Component({
selector: 'app-abs',
templateUrl: './abs.component.html',
styleUrls: ['./abs.component.css']
})
export class ABSComponent implements OnInit {
constructor(
private _API: ABSService
) {}
ngOnInit(){
this._API.getUsers().subscribe(
(data:any)=>{
// something related to data
this.dataObj = data;
console.log('dataObj-> ' + JSON.stringify(this.dataObj));
},
(err)=>{
// something related to error
}
)
}
}
I prefer to keep HTTP module separate since the last time when they changed the HTTP module with HttpClient module, I had to go through a lot of work.