Within my Angular 2 project, I am implementing the use of Http (@angular/http
) to communicate with my API. In order for these requests to be successful, specific headers, including a JWT header, must be included in each API request.
My goal is to have an API class handle the creation of Http requests, along with error handling, validation, and other tasks.
However, I am encountering an issue where I am unable to utilize the Http class within my API class, resulting in the following error:
https://i.sstatic.net/WX4kX.pnguser.service.ts
import { Injectable } from '@angular/core';
import {User} from "../models/User";
import {API} from "../API";
import {Http} from "@angular/http";
@Injectable()
export class UserService
{
constructor (private http : Http) {}
getProfile (user : User)
{
let api = new API (this.http);
return api.doRequest ('/user/' + user.id + '/profile');
}
}
API.ts
import {Http, Headers, RequestOptions} from '@angular/http';
export class API
{
...
constructor (private http : Http) {}
doRequest (url : string, method : string, data?)
{
let headers = {...};
let options = new RequestOptions ({ headers: new Headers (headers), ... } );
return this.http.get (url, data, options)
.catch ((error) => { ... } );
}
}
It seems that using Http directly from the UserService works more effectively. Is there a solution to this problem, or a more efficient approach to achieving the desired outcome? Would extending Http be a viable option?