调用 fixture.detectChanges 时未定义 enviromentService?
enviromentService is undefined when calling fixture.detectChanges?
我在 运行 执行此代码时出错:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ SharedTestingModule,
TranslateModule.forRoot({
loader: { provide: TranslateLoader, useValue: mockTranslateLoader }
})],
declarations: [ RequestInfoComponent, StaticContentComponent ],
providers: [{ provide: EnvironmentService, userValue: EnvironmentService }]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RequestInfoComponent);
component = fixture.componentInstance;
componentHtml = fixture.debugElement.nativeElement;
environmentService = fixture.debugElement.injector.get(EnvironmentService);
});
问题是 fixture.detectChanges()。如果我删除 fixture.detectChanges() 那么一切正常,但我应该 运行 代码。我无法删除它。
错误是:
TypeError: this.enviromentService is undefined
这是我在组件中的代码:
import { Component, OnInit } from '@angular/core';
import { EnvironmentService } from 'app/shared/environment.service';
@Component({
selector: 'app-request-info',
templateUrl: './request-info.component.html',
styles: []
})
export class RequestInfoComponent implements OnInit {
urlPrefix: string;
constructor(private environmentService: EnvironmentService) { }
ngOnInit() {
this.urlPrefix = this.environmentService.getWebContentUrlPrefix();
}
}
在第一个 beforeEach
块中,您拥有的第一个提供商的类型为:
{ provide: EnvironmentService, userValue: EnvironmentService }
如果 EnvironmentService
是一种类型,请尝试将 userValue
更改为 useClass
,如果它是一个值,请尝试将其更改为 useValue
。
同意@BeetleJuice 提供的解决方案。至于你的错误:
类型错误:this.enviromentService 未定义
这是因为您需要按以下方式进行作业
component.environmentService = fixture.debugElement.injector.get(EnvironmentService);
我在 运行 执行此代码时出错:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ SharedTestingModule,
TranslateModule.forRoot({
loader: { provide: TranslateLoader, useValue: mockTranslateLoader }
})],
declarations: [ RequestInfoComponent, StaticContentComponent ],
providers: [{ provide: EnvironmentService, userValue: EnvironmentService }]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(RequestInfoComponent);
component = fixture.componentInstance;
componentHtml = fixture.debugElement.nativeElement;
environmentService = fixture.debugElement.injector.get(EnvironmentService);
});
问题是 fixture.detectChanges()。如果我删除 fixture.detectChanges() 那么一切正常,但我应该 运行 代码。我无法删除它。
错误是:
TypeError: this.enviromentService is undefined
这是我在组件中的代码:
import { Component, OnInit } from '@angular/core';
import { EnvironmentService } from 'app/shared/environment.service';
@Component({
selector: 'app-request-info',
templateUrl: './request-info.component.html',
styles: []
})
export class RequestInfoComponent implements OnInit {
urlPrefix: string;
constructor(private environmentService: EnvironmentService) { }
ngOnInit() {
this.urlPrefix = this.environmentService.getWebContentUrlPrefix();
}
}
在第一个 beforeEach
块中,您拥有的第一个提供商的类型为:
{ provide: EnvironmentService, userValue: EnvironmentService }
如果 EnvironmentService
是一种类型,请尝试将 userValue
更改为 useClass
,如果它是一个值,请尝试将其更改为 useValue
。
同意@BeetleJuice 提供的解决方案。至于你的错误: 类型错误:this.enviromentService 未定义 这是因为您需要按以下方式进行作业
component.environmentService = fixture.debugElement.injector.get(EnvironmentService);