角度:在AOT中提供HTTP



我正在使用一个自定义的http客户端,我提供的函数作为supportfactory的依赖性。

{
  provide: TranslateLoader,
  useFactory: HttpLoaderFactory,
  deps: [Client, Url]
},

在JIT中工作正常,但是在AOT中,我收到此错误消息:

ERROR in C:/PATH/src/$$_gendir/app/app.module.ngfactory.ts (1,1): Argument of type 'Client' is not assignable to parameter of type 'Http'.
  Property '_backend' is missing in type 'Client'.

,但这没关系,因为它受到保护,并且无法从外部访问的属性,对

我该怎么办?将这些受保护的属性添加到我的客户端无法解决问题,我不知道如何使我的班级扩展HTTP而不破裂,因为在超级呼叫中,我需要通过我不知道它如何工作以及如何获得一个。

编辑:我尝试了扩展方法,但没有成功:扩展时,如果我有构造函数,则如下:

@Injectable()
export class Client extends Http {
constructor(private authService: AuthService,  providers: XHRBackend) {
    super(providers, null);
    this.onInit();
}

或喜欢

@Injectable()
export class Client extends Http {
constructor(private authService: AuthService) {
        super(null, null);
        this.onInit();
    }

将http methot覆盖为使人的

return super.get(url, this._getOptions(options)).catch((error: any) => this._handleError(error));

它会说:

zone.js:569 Unhandled Promise rejection: Cannot read property 'merge' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'merge' of null
    at mergeOptions (http.es5.js:1767)

添加提供商

    useFactory: (
        backend: XHRBackend,
        defaultOptions: RequestOptions) =>
        new CustomHttp(backend, defaultOptions),
    deps: [XHRBackend, RequestOptions]

另请参见如何覆盖RC6中的HTTP类?

好吧,我终于设法找到了该解决方案,以防其他人需要它:我像这样扩展了Angualr的HTTP类:

@Injectable()
export class Client extends Http {
constructor(private authService: AuthenticationService, providers: XHRBackend) {
    super(providers, new BaseRequestOptions ());
    this.onInit();
}

现在它在JIT和AOT中正常工作。

编辑:使用此解决方案,我仍然需要导入httpmodule,但是我使用的类与http不同,可以处理oauth2令牌。

最新更新