angular - 如何覆盖类构造函数?

标签 angular

我有 Foo 类:

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

以及扩展类 Foo 的类 Bar:

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

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

更新: 安全HTTP:

@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]
        }
    ]
})

最佳答案

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

  }

}    

关于angular - 如何覆盖类构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40173556/

相关文章:

angular - 如何更改 Angular2 中的特定内部路由参数

angular - Angular-Cli 生成的文件夹中没有 NgModule

angular - 收到错误 : Could not find column with id "id"

javascript - 错误 : document is not defined

javascript - 如何使用子组件使用 Tabset 组件创建动态选项卡?

unit-testing - Angular 2 : MockBackend returning 'undefined' response

angular - NgRx:如何在 meta-reducers 中使用服务?

javascript - Angular - 订阅使用 http 的服务

angular - ngbPopover 没有出现

javascript - 如何从 json() 对象访问键/值对?