How to redirect from parent route to child route in Angular canActivate guard?

user3887366

I've got this routes

AppRouting

{
   path: 'user',
   canLoad: [AuthGuard],
   loadChildren: () =>
   import('./user/user.module').then((m) => m.PublicUserModule)
}

UserRouting

{
    path: '',
    component: PublicUserPageComponent,
    canActivate: [UserPhonesCheckGuard],
    children: [
      /*{
        path: '',
        pathMatch: 'full',
        redirectTo: 'check'
      },*/
      {
        path: 'account',
        loadChildren: () =>
          import('./account/user-account.module').then(
            (m) => m.PublicUserAccountModule
          )
      },
      {
        path: 'check',
        loadChildren: () =>
          import('./check/user-check.module').then(
            (m) => m.PublicUserCheckModule
          )
      }
    ]
  }

Using UserPhonesCheckGuard depending on some conditions I want to redirect or to the check or to account children route but with

canActivate() 
    return this.router.parseUrl('/user/check');
  }

the browser go nuts :(

What path should I use?

ysf

In this way;

canActivate() 
    return this.router.parseUrl('/user/check');
}

an infinite loop occurs.

Because when you return a UrlTree (which is returned by this.router.parseUrl) from canActivate current navigation is cancelled and a new navigation started.

Since new navigation is going to a sub url (child) of current navigation, canActivate guard runs again for the new navigation which in turn causes an infinite loop.

That's why you need a way to detect child navigation in canActivate and break the infinite loop. One way of detecting child navigation can be to control the url. Such as;

canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot) {
  console.log(state.url)

  if(state.url === "/user/check" || state.url === "/user/account") 
    return true;

  if(someCondition) /* here you determine which child to navigate */
    return this.router.parseUrl('/user/check');
  else
    return this.router.parseUrl('/user/account');
}

I created a simple demo here. In the demo, you can see in the console that canActivate runs twice for every navigation. One for the parent navigation and one for the child navigation. Without if condition, parent navigation would run indefinitely.

I hope it helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I navigate to a parent route from a child route?

How can I access an activated child route's data from the parent route's component?

How to get :id param of child route from parent component in Angular2

angular2 - Pass value from parent route to child route

How can you pass data from a parent route to a child route in Vue.js nested routes?

Will Angular 2 child routes refresh the parent route

How to get active child route in parent component Angular 2

How to access parent component properties from a child route component in Angular?

Get target route URL in CanActivate guard Lazy load modules Angular

Rendering a child route in the parent in Angular 4

Angular default child route not loading parent component

Vue navigate from child route to parent of parent route not working

Web Api Call from an Angular Route Guard

Angular Route Guard CanActivate stops routing

React - How to route parent div from child div

Child route reloading without getting data from parent route

Angular: How to navigate to child route from parent component by using navigate method

How to access route params in route guard clause in Angular 7?

How do I navigate from a React Child route back to a parent

Read cookie in CanActivate route guard in Angular

Ember - How to link from parent route to child route when both have array controllers?

How I make two ts files for parent route and child route in Angular2?

Angular 2 Route Guard CanActivate based on Firebase User Role

How to use CanActivate for child route correctly?

Passing state from parent to child route

Angular Router route guard CanActivate always returns false

How to get the immediate parent route URL when we are at a child route in angular?

How to automatically route to a child route in Angular

How to redirect from a PATCH route to a POST route?