最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular router.navigate w route guard not working when already on Component - Stack Overflow

programmeradmin4浏览0评论

I am trying to figure out why the router.navigate(['']) code below does not take the user to the login ponent. I just stay on the home ponent. When I added debugger; to the code it seems like it goes into some sort of endless loop.

Here is the process: User es to site and instantly gets redirected to /login since the Route Guard fails. If they login, Route Guard passes and they go to [' '] which is the HomeComponent. Then when they click logout I figure the navigate to [' '] should fail and simply go to /login. For some reason it stays on the HomeComponent and never navigates.

homeponent.ts

  logout() {
    this.userService.logout();
    this.router.navigate(['']);
  }

user.service.ts

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;
    return this.verifyLogin(url);
  }

  verifyLogin(url: string): boolean {
    if (this.userLoggedIn) { return true; }

    this.router.navigate(['/login']);
    return false;
  }

  logout() {
    this.userLoggedIn = false;
  }

app-routing.module.ts

const routes: Routes = [
    { path: '' , ponent: HomeComponent, canActivate: [UserService]},
    { path: 'login', ponent: LoginComponent },
    { path: 'register', ponent: RegisterComponent },
    { path: '**' , redirectTo: '' }
];

I am trying to figure out why the router.navigate(['']) code below does not take the user to the login ponent. I just stay on the home ponent. When I added debugger; to the code it seems like it goes into some sort of endless loop.

Here is the process: User es to site and instantly gets redirected to /login since the Route Guard fails. If they login, Route Guard passes and they go to [' '] which is the HomeComponent. Then when they click logout I figure the navigate to [' '] should fail and simply go to /login. For some reason it stays on the HomeComponent and never navigates.

home.ponent.ts

  logout() {
    this.userService.logout();
    this.router.navigate(['']);
  }

user.service.ts

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    let url: string = state.url;
    return this.verifyLogin(url);
  }

  verifyLogin(url: string): boolean {
    if (this.userLoggedIn) { return true; }

    this.router.navigate(['/login']);
    return false;
  }

  logout() {
    this.userLoggedIn = false;
  }

app-routing.module.ts

const routes: Routes = [
    { path: '' , ponent: HomeComponent, canActivate: [UserService]},
    { path: 'login', ponent: LoginComponent },
    { path: 'register', ponent: RegisterComponent },
    { path: '**' , redirectTo: '' }
];
Share Improve this question edited Apr 30, 2017 at 18:35 Blake Rivell asked Apr 30, 2017 at 18:15 Blake RivellBlake Rivell 13.9k33 gold badges130 silver badges260 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

For those using Angular 5+, you CAN get router.navigate(['']) to reload the same URL. It's not well documented, and ing from a server heavy framework like .NET, this feels more natural.

Added runGuardsAndResolvers: 'always' to one of your paths and onSameUrlNavigation: 'reload' to the RouterModule initialization.

app-routing.module.ts

const routes: Routes = [
    { path: '' , ponent: HomeComponent, canActivate: [UserService], runGuardsAndResolvers: 'always' },
    { path: 'login', ponent: LoginComponent },
    { path: 'register', ponent: RegisterComponent },
    { path: '**' , redirectTo: '' }
];

@NgModule({
    imports: [RouterModule.forRoot(routes, {
        onSameUrlNavigation: 'reload'
    })],
    exports: [RouterModule]
})
export class AppRoutingModule { }

Why it should navigate it is already in [' '] and you ask again go to [' ']? You can call 'this.router.navigate(['/login'])' in logout.

logout() {
   this.userLoggedIn = false;
   this.router.navigate(['/login']);

}
发布评论

评论列表(0)

  1. 暂无评论