angularangular-routingangular-routerangular-routerlink

How to know if angular router has registered route


I'm migrating a set of Anguar microsites info module federation based MFE architecture.

Not all apps will be migrated at once, so I have this issue to solve:

So far, all cross-microsite links were just regular a tags with href and full page reload was done.

Now, as MFEs are being loaded into the shell one by one, sometimes links will point to valid angular route (if MFE was lazy loaded using module federation), but sometimes not.

So, sometimes link would need to be rendered using routerLink directive and sometimes using href.

Is there a way to tell if a route e.g. apps/admin is valid Angular route at runtime in order to decide whether to use routerLink or href?


Solution

  • I'm not sure what you really want, but you can use router.config and loop over it to check if certain path exists or not

      constructor(private router: Router) {
        console.log(this.router.config);
      }
    
    
      routeExists(path: string): boolean {
        return this.router.config.some((route) => route.path === path)
      }