【问题标题】:AHEAD-OF-TIME COMPILATION Issue提前编译问题
【发布时间】:2017-03-06 12:14:13
【问题描述】:

我使用了 AHEAD-OF-TIME 编译, 编译AOT文件夹成功生成后,但在app.modulengfactory.ts文件中出现问题,它会抛出类似的错误 构建:通用类型“HttpService”需要 1 个类型参数。当我构建项目时

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

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

【问题讨论】:

    标签: angular angular2-aot


    【解决方案1】:

    尝试从 HttpService 中删除 Generic。

    HttpService replace HttpService<T>
    

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

    【讨论】:

    • 我无法从 httpservice 中删除 genric,因为我需要通用服务。
    【解决方案2】:

    像这样改变你的服务:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多