简体   繁体   English

角度路由设置问题

[英]Angular routing setup issue

I have an Angular library that includes its own routing.我有一个包含自己路由的 Angular 库。

The expected scenario is:预期的场景是:

  • The user clicks on the 'Add remote component' button.用户单击“添加远程组件”按钮。
  • The CurrentCasesComponent component (library) loads, then it redirects to the login screen. CurrentCasesComponent 组件(库)加载,然后重定向到登录屏幕。
  • The user clicks 'Authenticate', the RemoteBComponent displays inside the tab.用户单击“身份验证”,RemoteBComponent 显示在选项卡内。

Observed behavior is:观察到的行为是:

  • The user clicks on the 'Add remote component' button => nothing displays.用户单击“添加远程组件”按钮 => 没有任何显示。
  • The user clicks on the 'Add remote component' button a second time => CurrentCasesComponent loads successfully.用户再次单击“添加远程组件”按钮 => CurrentCasesComponent 加载成功。
  • The user clicks authenticate => the remote screen displays successfully.用户单击身份验证 => 远程屏幕显示成功。

I import this library in my main app at runtime using SystemJS and I have some issues figuring out what the Angular routing setup should be.我在运行时使用 SystemJS 将这个库导入到我的主应用程序中,但在确定 Angular 路由设置应该是什么时遇到了一些问题。

The library's routing is:图书馆的路线是:

const routes: Routes = [
  {
    path: "current",
    component: CurrentCasesComponent
  },
  {
    path: "login",
    component: LoginComponent
  },
  { path: "**", redirectTo: "current", pathMatch: "full" },
];

@NgModule({
  declarations: [],
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule]
})
export class CasesRoutingModule {}

The main app's routing is:主应用程序的路由是:

const routes: Routes = [
  {
    path: "pi",
    component: PiComponent,
    children: [
      {
        path: "dynamic",
        component: DynamicPackComponent,
        children: [{ path: "", component: DynamicPackComponent }]
      },
      { path: "static", component: StaticComponent }
    ]
  },
  {
    path: "static",
    component: StaticComponent
  },
  { path: "**", redirectTo: "pi", pathMatch: "full" }
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { enableTracing: true })],
  exports: [RouterModule]
})
export class AppRoutingModule {}

I insert the remote routing under the 'pi' path in the main routing when loading the remote component.加载远程组件时,我将远程路由插入到主路由的“pi”路径下。 No issue with that: the router looks like this:没有问题:路由器看起来像这样:

路由器

  export class CurrentCasesComponent implements OnInit, AfterViewInit {
  constructor(private router: Router, private route: ActivatedRoute) {}

  ngAfterViewInit(): void {
    this.router.navigate(["./remote/login"], { relativeTo: this.route });
  }

I can see that the following routing call is issued:我可以看到发出了以下路由调用:

NavigationEnd {id: 2, url: "/pi/remote/login", urlAfterRedirects: "/pi/remote/login"}

I am missing something with the routing setup and any advice is welcome.我在路由设置中遗漏了一些东西,欢迎提供任何建议。

Here is a link to Stackblitz to demonstrate the issue.这里有一个指向 Stackblitz 的链接来演示这个问题。 stackblitz 闪电战

Note that if you comment out the following routing call to the login, the 'CurrentCaseComponent' displays successfully once you click on the 'Add remote component' button.请注意,如果您注释掉以下对登录的路由调用,则单击“添加远程组件”按钮后,“CurrentCaseComponent”将成功显示。

export class CurrentCasesComponent implements OnInit, AfterViewInit {
  constructor(private router: Router, private route: ActivatedRoute) {}

  AfterViewInit(): void {
    //this.router.navigate(["../remote/login"], { relativeTo: this.route });
  }

I finally figured it out.我终于弄明白了。 Once the remote component loads, it fires the following call:一旦远程组件加载,它会触发以下调用:

NavigationEnd {id: 2, url: "/pi/remote/login", urlAfterRedirects: "/pi/remote/login"}

which triggers the reloading of the PiComponent which selects the first tab per default thus overwriting the remote tab.这会触发 PiComponent 的重新加载,它默认选择第一个选项卡,从而覆盖远程选项卡。 The solution is to manage the tabs outside the component.解决方案是管理组件外的选项卡。 The following link demonstrates the solution.以下链接演示了解决方案。

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

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