发出路由器时出现无限重定向循环。使用Angular2 Router导航

Sam*_*ara 5 office-js angular2-routing angular

注意使用相关代码段进行编辑

我遇到了一个奇怪的问题,在发出router.navigate时进入了无限重定向循环。

设定

  • 我使用哈希位置策略,该应用程序用作Outlook插件。
  • 我有三个路由规则
    • “”重定向到“ /登录”
    • “ /登录”映射到LoginViewComponent
    • “ / foo”映射到FooViewComponent
  • LoginViewComponent具有两个行为:

    • 如果无法验证用户身份,将提示用户输入凭据
    • 如果不是,则将重定向发送到FooViewComponent
  • 重定向仅通过以下逻辑位发出:

this.router.navigate(["foo"])

  • 路线注册如下:

const routes: Routes = [
    {
       path: "",
       redirectTo: "login",
       pathMatch: "full"
    },
    {
        path: "login",
        component: LoginViewComponent
    },
    {
        path: "foo",
        component: FooComponentView
    }
];

@NgModule({
    imports: [RouterModule.forRoot(routes, { useHash: true,  enableTracing: true})],
    exports: [RouterModule]
})
export class AppRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

问题

  • 当我在LoginViewComponent的ngOnInit函数中发出重定向时,我进入了无限重定向循环。
  • 它首先导航到FooViewComponent,然后重定向到LoginViewComponent。
  • 据我了解,只有在调用router.navigate([“”])或router.navigate([“ login”])的情况下,才能重定向到LoginViewComponent。但是,在FooViewComponent中都没有这些导航命令。

angulart2-路由器无限重定向环路