I have used AHEAD-OF-TIME COMPILATION, After compilation AOT folder is generated suceessfully, but issue in app.modulengfactory.ts file, it throws error like Build:Generic type 'HttpService' requires 1 type argument(s). When I build Project
My app.Module.ts file is as below
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { HttpModule, XHRBackend } from "@angular/http";
import { routing } from "./app.routes";
import { HttpService } from "./http.service";
import { LoginService } from "./login.service";
import { LoaderService } from "./shared/loader.service";
import { SharedModule } from "./shared/shared.module";
import { MaterialModule } from '@angular/material';
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule,
HttpModule,
routing,
MaterialModule,
SharedModule
],
bootstrap: [AppComponent],
providers: [HttpService,LoginService, LoaderService],
})
export class AppModule { }
I think issue is that I have not passed parameters in httpservice which accepts generic class as parameter.
My Http Service is as below
import {Http, Headers, RequestOptions, Response} from "@angular/http";
import {Injectable} from "@angular/core";
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/Rx';
@Injectable()
export class HttpService<T> {
constructor(private http: Http) {
}
get(url: string) {
return this.intercept(this.http.get(url).map(res => res.json()));
}
post(url: string, body: T) {
let jsonBody = JSON.stringify(body);
let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' });
let options = new RequestOptions({ headers: headers });
return this.intercept(this.http.post(url, jsonBody, options).map(res => res.json()));
}
put(url: string, body: T) {
let jsonBody = JSON.stringify(body);
let headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
let options = new RequestOptions({ headers: headers });
return this.intercept(this.http.put(url, jsonBody, options).map(res => res.json()));
}
delete(url: string) {
return this.intercept(this.http.delete(url).map(res => res.json()));
}
intercept(observable: Observable<Response>) {
return observable.catch((err, source) => {
if (err.status === 401) {
location.href = "/Login";
}
else if (err.status === 500) {
return Observable.throw(err);
}
else {
return Observable.throw(err);
}
});
}
}
So please give any suggestion how can i solve this issue.
Try to remove the Generic from HttpService.
And plus,its better just use HttpService,do not wrap it.