Я строю свою службу api. Я хочу, чтобы он был универсальным. Как определить apiUrl в определении поставщика?Передача параметров на обслуживание
Вот моя служба:
import {Injectable} from "@angular/core";
import {Http, Response} from "@angular/http";
import '/js/admin/rxjs-operators';
import {Observable} from "rxjs";
@Injectable()
export class ApiService {
private apiUrl: string = 'http://www.system.local/api/';
constructor(private http: Http, private apiUrl: string) {
}
get(url: string, params): Observable<Response> {
let requestUrl: string = this.apiUrl + url + this.parseParams(params);
return this.http.get(requestUrl)
.map(response => response.json());
}
post(url: string, params): Observable<any> {
let requestUrl: string = this.apiUrl + url;
return this.http.post(requestUrl, params)
.map(response => response.json());
}
}
Я пытался с поставщиком услуг, но, кажется, что deps
должен быть класс:
import {ApiService} from "./api.service";
import {Http} from "@angular/http";
let apiServiceFactory = (http: Http, apiUrl: string)=> {
return new ApiService(http, apiUrl);
};
export let apiServiceProvider = {
provide: ApiService,
useFactory: apiServiceFactory,
deps: [Http, 'http://www.wp.pl']
};
и в модуле: @NgModule ({ поставщики : [ apiServiceProvider ] )
Возможный дубликат [Угловой 2 «Нет провайдера для строки!»] (Http://stackoverflow.com/questions/39628768/angular-2-no-provider-for-string) –