我的appModule看起来或多或少是这样的:
@NgModule({
declarations: [ ],
providers: [
WINDOW_PROVIDER,
{ provide: XHRBackend, useClass: AuthenticationConnectionBackend },
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
WINDOW_PROVIDER的位置如下:
import { ValueProvider } from '@angular/core';
export const WINDOW_PROVIDER: ValueProvider = {
provide: 'Window',
useValue: window
};
最后,AuthenticationConnectionBackend就是这个:
export class AuthenticationConnectionBackend extends XHRBackend {
private window;
constructor(_browserXhr: BrowserXhr, _baseResponseOptions: ResponseOptions,
_xsrfStrategy: XSRFStrategy, @Inject('Window') injectedWindow: Window) {
super(_browserXhr, _baseResponseOptions, _xsrfStrategy);
// injectedWindow = undefined;
this.window = injectedWindow;
}
}
问题是,一旦angular运行AuthenticationConnectionBackend构造函数, injectWindow 值未定义,我需要将其分配给服务的某个变量才能进行测试上课。
谢谢!如果您需要一些额外的信息,请不要犹豫!