Infinite redirect loop when issuing a router.navigate with Angular2 Router

我是研究僧i 提交于 2019-12-21 16:58:29

问题


Note Edit with relevant code snippets

I have run into an odd issue where I get into a infinite redirect loop when issuing a router.navigate.

Setup

  • I use the hash location strategy and the app is used as an Outlook plugin.
  • I have three routing rules
    • "" redirect to "/login"
    • "/login" mapped to LoginViewComponent
    • "/foo" mapped to FooViewComponent
  • The LoginViewComponent has two behaviours:

    • If the user's identity cannot be verified the user will be prompted for credentials
    • If not redirect is issued to the FooViewComponent
  • The redirect is simply issued with the following bit of logic:

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

  • The routes are registered as follows:

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 { }

Problem

  • When I issue a redirect in the ngOnInit function of the LoginViewComponent I get into an infinite redirect loop.
  • It first navigates to the FooViewComponent but then does a redirect back to the LoginViewComponent.
  • As far as I can understand the redirect to the LoginViewComponent can only be made if there is a call to router.navigate([" "]) or router.navigate(["login"]). However, neither of these navigate commands are present in the FooViewComponent.

来源:https://stackoverflow.com/questions/44719730/infinite-redirect-loop-when-issuing-a-router-navigate-with-angular2-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!