Reputation: 1
I'm trying to use my ApiService class (handling api requests) in another class, but can't get it to work.
The issue is the ApiService constructor needs HttpClient, that means I can't just use something like: http = new ApiService(new HttpClient(), globals)
ApiService:
import { Injectable } from '@angular/core';
import { HttpClient, HttpRequest } from '@angular/common/http';
import { Globals } from './globals';
@Injectable({
providedIn: 'root'
})
export class ApiService {
constructor(private http: HttpClient, private globals: Globals) {}
get(url : string, params: {}){
return this.http.get(this.globals.api_url.concat(url), {params: params});
}
...
}
Class calling ApiService:
export class UploadAdapter {
http: ApiService;
constructor() {}
upload() {
//Here I get an error saying can't get post of undefined
http.post('api_url'.concat('/medias/upload'), {}, {})
.subscribe((data) => {
//do stuff here.
});
}
}
Upvotes: 0
Views: 138
Reputation: 4378
You are not injecting the service in your component
Your UploadAdapter
constructor should be like this
constructor(private http: ApiService) {}
Also you need to use
this.http.post
Instead of
http.post
Upvotes: 1