AHEAD-OF-TIME COMPILATION Issue

269 views Asked by At

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.

2

There are 2 answers

1
胡亚雄 On

Try to remove the Generic from HttpService.

HttpService replace HttpService<T>

And plus,its better just use HttpService,do not wrap it.

0
coderdark On

Change your service like this:

import {Injectable} from '@angular/core'
import {HttpModule, Http,Response} from "@angular/http";
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class HttpService { 

    constructor(private http: Http) {
    }

    getUser(url: string):Observable<any> {
        return this.http.get(url).map((res:Response) => {
            return res.json();
        });
    }
}

Here is the punklr: https://plnkr.co/edit/7T3Qiv0jfXiIWT0BPp0e?p=preview

You can also see an example of a service here; https://angular.io/docs/ts/latest/guide/server-communication.html#!%23http-client