Javascript typeerror:this.notificationeventservice.shownotification.subscribe

Javascript typeerror:this.notificationeventservice.shownotification.subscribe,javascript,angular,unit-testing,jasmine,karma-jasmine,Javascript,Angular,Unit Testing,Jasmine,Karma Jasmine,组件网文件 ngOnInit(): void { this.subscribeNotification(); } subscribeNotification { this.notificationEventService.showNotification.subscribe((data: NotificationData) => { console.log(data); }); } 服务文件 import { EventEmitter, Injectable } fro

组件网文件

ngOnInit(): void {
 this.subscribeNotification();
}
subscribeNotification {
  this.notificationEventService.showNotification.subscribe((data: NotificationData) => {
    console.log(data);
  });
}
服务文件

import { EventEmitter, Injectable } from '@angular/core';

@Injectable()
export class NotificationEventService {
   public notificationCounter = 0;

   showNotification = new EventEmitter();

   notifySuccess(message: string) {
   const data = { type: 'banner', severity: 'success', discreet: false, text: message };
   this.showNotification.emit(data);
}
 }
元件的等级库文件

const notificationEventServiceStub = () => ({
  showNotification: ()  => ({ subscribe: f => f({}) })
});
TestBed.configureTestingModule({
  imports: [HttpClientTestingModule],
  providers: [{ provide: NotificationEventService, useFactory: notificationEventServiceStub}]
});
describe('ngOnInit', () => {
it('makes expected calls', () => {
  spyOn(component, 'subscribeNotification').and.callThrough();
  component.ngOnInit();
  expect(component.subscribeNotification).toHaveBeenCalled();
});
});
一旦我监视了这个ngOnInit函数,我就会


TypeError:this.notificationEventService.showNotification.subscribe不是一个函数

有一种更好的方法可以做到这一点,您可以使用Behavior Subject rxjs并订阅它。
要更改其值,您必须使用next()方法,而不是emit()方法。您可以这样做。
下面是一个如何使用行为主题RXJS的示例

希望这有帮助,快乐编码:)


您应该监视
EventEmitter
subscribe
方法,而不是
EventEmitter
对象。您是否在ModuleEyes的提供程序中添加了服务?我实际上添加了此错误,我在Jasmine中遇到了此错误?使用event emmiter是否不可能?