Daniel
Daniel

Reputation: 2015

check whether a url is part of angular application

I have a web app that was partly migrated from PHP and angular-js to angular(currently v13) after a successful login, there's a redirection to other pages. since my login page is angular-based, I'd like to use angular routing to redirect to pages that are angular-based and are working under the router-outlet. And redirect using window.location for old pages (e.g PHP).

The url to redirect to is dynamic. Is there a way that I can determine if the url is part of the angular application and can be navigated to using the router?

Upvotes: 2

Views: 863

Answers (1)

Anton Marinenko
Anton Marinenko

Reputation: 2992

You can use router events for that. Do this code in a service or your root navigation component:

constructor(private router: Router) {
    router.events.pipe(
      filter(event => event instanceof NavigationError),
      takeUntil(this.destroy),
    ).subscribe(.. here you should redirect with window.location);
  }

Also if you needn't error in the console, just provide custom error handler in providers:

providers: [{
    provide: ErrorHandler,
    useClass: CustomErrorHandler,
}],

And one last moment: you have to remove '**' path parsing from your root routes in RouterModule.

Upvotes: 2

Related Questions