我有 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/