Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

After redirecting angular page by SignalR event Component not working

I have a method named "leave()" in which, we are redirecting the page to dashboard.
Whenever I uses this method by button click, it’s working fine.

There is another scenario where it called by signalr event(Subject). In this case, page is redirecting successfully, but components on the dashboard page not loading properly.

In below code "OnLeaveAll" is Subject type in "hubService".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ngOnInit(): void {
    this.hubService.OnLeaveAll.subscribe(x => {
      this.leave();
    });
}

leave() {
    this.router.navigate(['app', 'dashboard']);
}

>Solution :

Check your devtools console. Maybe there is an error regarding the angular zone.
If yes, try this.

import { NgZone } from '@angular/core';

constructor(private ngZone: NgZone) {
}

ngOnInit(): void {
  this.hubService.OnLeaveAll.subscribe(x => {
    this.ngZone.run(() => {
      this.leave();
    });
  });
}

leave() {
  this.router.navigate(['app', 'dashboard'], { replaceUrl: true });
}

You can find more about this on below link.

https://angular.io/guide/zone#ngzone-run-and-runoutsideofangular

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading