CanActivate vs. CanActivateChild with component-less routes

后端 未结 6 1159
傲寒
傲寒 2020-12-12 18:17

The angular2 documentation about Route Guards left me unclear about when it is appropriate to use a CanActivate guards vs. a CanActivateChild guard

6条回答
  •  情深已故
    2020-12-12 18:44

    From the docs:

    As we learned about guarding routes with CanActivate, we can also protect child routes with the CanActivateChild guard. The CanActivateChild guard works similarly to the CanActivate guard, but the difference is its run before each child route is activated. We protected our admin feature module from unauthorized access, but we could also protect child routes within our feature module.

    Here's a practical example:

    1. navigating to /admin
    2. canActivate is checked
    3. You navigate between the children of /admin route, but canActivate isn't called because it protects /admin
    4. canActivateChild is called whenever changing between children of the route its defined on.

    I hope this helps you, if still unclear, you can check specific functionality by adding guards debugging them.

提交回复
热议问题