如何覆盖类构造函数?

时间:2016-10-21 09:54:15

标签: angular

我上课Foo:

export class Foo {
   constructor(protected http: Http) {
   }
   ...
}

出类Foo的类吧:

export class Bar extends Foo {
   constructor(protected http: SecurityHttp) {
      super(http);
   }
   ...
}

问题是在类Bar中注入无效的Http实例(默认Http而不是SecurityHttp)。 当我尝试使用SecurityHttp在任何组件类中创建构造函数时,它运行良好。

更新: SecurityHttp:

@Injectable()
export class SecurityHttp extends Http {

    constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
        super(backend, defaultOptions);
    }
}

模块:

@NgModule({
   providers: [
        {
            provide: SecurityHttp,
            useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions) => {
               return new SecurityHttp(xhrBackend, requestOptions, authService, router);
            },
            deps: [XHRBackend, RequestOptions]
        }
    ]
})

1 个答案:

答案 0 :(得分:0)

export class BaseComponent {

constructor(protected utilitiesService: UtilitiesService,
    protected loggingService: LoggingService) {
    this.logNavigation();
}

protected logError(errorMessage: string) { . . .}
private logNavigation() { . . .}

}

export class ChildComponent extends BaseComponent {

constructor(private childDataService: ChildDataService,

utilitiesService: UtilitiesService,

              loggingService: LoggingService) {

super(utilitiesService, loggingService);

  }

}