0

我使用了 AHEAD-OF-TIME 编译,编译 AOT 文件夹成功生成后,但在 app.modulengfactory.ts 文件中出现问题,它会引发错误,例如 Build:Generic type 'HttpService' requires 1 type argument(s)。当我构建项目时

我的 app.Module.ts 文件如下

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 { }

我认为问题是我没有在接受泛型类作为参数的 httpservice 中传递参数。

我的 Http 服务如下

 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);
        }
    });
   }
}

所以请给我任何建议我该如何解决这个问题。

4

2 回答 2

0

尝试从 HttpService 中删除 Generic。

HttpService replace HttpService<T>

另外,最好只使用 HttpService,不要包装它。

于 2016-12-15T07:02:08.597 回答
0

像这样更改您的服务:

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();
        });
    }
}

这是朋克:https ://plnkr.co/edit/7T3Qiv0jfXiIWT0BPp0e?p=preview

您还可以在此处查看服务示例;https://angular.io/docs/ts/latest/guide/server-communication.html#!%23http-client

于 2016-12-15T07:22:48.913 回答