1

I created a service for getting SSE(server send events) from backend(Node). When I use this service in one component it's working as expected. But when I try to use in multiple components at very first time ngoninit triggers and loads the data perfectly. When I navigate to one component to another the ngoninit is not triggering and even the services are also not being called. I hope someone can help on this.

Thanks in advance.

  • Angular services are singleton, will get initiated only once in web page lifetime (i.e, until page is manually reloaded by user). If your logic is written in constructor of service then this behavior is possible – Wazeed Apr 12 '21 at 11:34
  • @Wazeed I have my logic outside the constructor i.e simple method which return an observable. So i am just calling the method subscribe in three different components. It completely stops execution of other services as well i.e apart from the events service. – Yagnes Yagne Apr 12 '21 at 13:01
  • 1
    We need to see the code to be able to help, because at this point I'm still not sure what the issue is – Bertramp Apr 12 '21 at 13:13

1 Answers1

0

Sounds like your components are not removed from the view and therefore not destroyed. Then ngOnInit will not trigger, because an instance of the component already exists

Bertramp
  • 376
  • 1
  • 15
  • But when I use the service in single component it works as expected. When I navigate between any two components ngoninit triggered and loads the data perfectly – Yagnes Yagne Apr 12 '21 at 13:03