2

I have routing as below:

{
  path: "home",
  children: [{
    path: "dashboard",
    children: [{
      path: "user",
      canDeactivate: [CanWeDeactivateThis]
    }]
  }]
}

There are components defined in routes, above is just a simple snapshot. I get component as null when I add can deactivate in user path but if I add it on dashboard path it works well. Adding in the dashboard does not work for me, as I have multiple children on the dashboard and I need show confirmation if the user navigates to them as well.

Not sure how to resolve this one.

The angular version is 6.1.0

I followed this thread but no help.

Cany anyone help please?

BTW, the above routing definitions is part of a lazy loaded angular module.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
  • What do you mean by `getting component as null`. Are you getting any error.. please provide some error details – Sachin Gupta Dec 06 '18 at 05:43
  • In `canDeactivate(component: CanComponentDeactivate) {` method of guard, I get first pararmeter `component` as null instead of actual component. –  Dec 06 '18 at 07:49

1 Answers1

0
{
  path: "home",
  children: [{
    path: "dashboard",
    children: [{
      path: "user",
      component : "user",
      canDeactivate: [CanWeDeactivateThis]
    }]
  }]
}

You Should add component name for the children. You can also try below code if you want to apply canDeactivate method to all the children.

{
      path: "home",
      children: [{
        path: "dashboard",
        canDeactivate: [CanWeDeactivateThis],
        children: [{
          path: "user",
          component : "user"          
        }]
      }]
    }

If the feature module is loaded asynchronously, the CanLoad guard is checked before the module is loaded. If any guard returns false, pending guards that have not completed will be canceled, and the entire navigation is canceled. So check if you have any CanLoad guard also.

Prithivi Raj
  • 2,658
  • 1
  • 19
  • 34
  • As I have mentioned in the question, "There are components defined in routes, above is just a simple snapshot." and I can not move the can deactivate as dashboard level as I want to monitor changes in children of the dashboard as well. –  Dec 06 '18 at 07:51
  • I do not have canLoad guard. –  Dec 06 '18 at 08:49