简体   繁体   English

如何知道angular路由器是否有注册路由

[英]How to know if angular router has registered route

I'm migrating a set of Anguar microsites info module federation based MFE architecture.我正在迁移一组基于 MFE 架构的 Anguar 微型站点信息模块联合。

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.到目前为止,所有跨微型站点的链接都只是带有 href 的常规a标签,并且已完成整页重新加载。

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.现在,随着 MFE 被一个接一个地加载到 shell 中,有时链接会指向有效的 angular 路由(如果 MFE 是使用模块联合延迟加载的),但有时不是。

So, sometimes link would need to be rendered using routerLink directive and sometimes using href .因此,有时需要使用routerLink指令呈现链接,有时需要使用href

Is there a way to tell if a route eg apps/admin is valid Angular route at runtime in order to decide whether to use routerLink or href ?有没有办法在运行时判断一条路由(例如apps/admin )是否有效 Angular 路由,以便决定是使用routerLink还是href

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我不确定你真正想要什么,但你可以使用router.config并循环检查它是否存在某些路径

  constructor(private router: Router) {
    console.log(this.router.config);
  }


  routeExists(path: string): boolean {
    return this.router.config.some((route) => route.path === path)
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM