1

I have an event emitter and 5 subscriber's in each of the 5 pages say(Page 1, Page 2, Page 3, Page 4, Page 5)

I subscribed for the event on the ngOnInit() and unsubscribe it on ngOnDestroy(),

These pages are actually routed pages , ie only only one page will be loaded to the DOM at once

So my use case is Initially i go to Page 1 and triggered the event emitter Then i move to Page 2 again triggered the event emitter I can observe that this event emitter triggered the subscription in both Page 1 and Page 2,

How it is possible because i have already unsubscribed, How can i fix this ?

ngOnInit() {
this.translationService = this.sharedService.language_english.subscribe(
      data => {
        console.log(data)
        this.trigger_translation_module(data);
      }
    );
}

 ngOnDestroy() {

    this.translationService.unsubscribe();
  }

1 Answers1

1

I believe that angular route changes don't call the destroy lifecycle methods.

Workarounds are discussed in the issue below https://github.com/NativeScript/nativescript-angular/issues/1049

If you don't need to specifically change the service depending on the page you could simply initialise it during your app.component.ts constructor instead of within each individual page

chrismclarke
  • 1,995
  • 10
  • 16