K
K
Konstantin2020-04-21 13:37:26
Angular
Konstantin, 2020-04-21 13:37:26

Why didn't the Subject event work?

Shipping:

private messageQueue = new AsyncSubject<IMessage>();

  public publish(message: IMessage): void {
    this.messageQueue.next(message);
  }

  public listen(): Observable<any> {
    return this.messageQueue.asObservable();
  }


Listen elsewhere:

constructor(private messageService: MessageService) {
    this.messageService.listen().subscribe((message: IMessage) => {
      console.log(message);
    });
  }


Nothing comes. Maybe listening to the observer in the service constructor is not possible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2020-04-21
@Junart1

Because you took AsyncSubject

A variant of Subject that only emits a value when it completes.

And
this.messageQueue.complete()
you don't have any.
listener listener in service constructor not possible?

When the body of the constructor is executed, all its dependencies are already fixed, i.e. the MessageService instance has already been successfully created by the injector.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question