{"id":685,"date":"2023-03-22T05:00:00","date_gmt":"2023-03-22T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=685"},"modified":"2023-03-21T15:14:34","modified_gmt":"2023-03-21T22:14:34","slug":"route-guards-canactivate-canactivatechild-candeactivate","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/route-guards-canactivate-canactivatechild-candeactivate\/","title":{"rendered":"Route guards &#8211; canActivate, canActivateChild, canDeactivate"},"content":{"rendered":"\n<p>Yesterday, we introduced <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-use-angular-route-guards\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how route guards can control whether or not a user can navigate<\/a> to a new URL in our application.<\/p>\n\n\n\n<p>The main guard to achieve that behavior is <code><a href=\"https:\/\/angular.io\/api\/router\/CanActivateFn\" target=\"_blank\" rel=\"noopener\" title=\"\">canActivate<\/a><\/code>. This guard will run every time the user tries to access a path and only allow the user to proceed if the guard function returns <code>true<\/code>.<\/p>\n\n\n\n<p>Here&#8217;s an example that&#8217;s going to check if the user is logged in before proceeding:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"112\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-31.png\" alt=\"\" class=\"wp-image-686\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-31.png 727w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-31-300x46.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If we want to redirect the user to a login component when they&#8217;re not logged in, we can do so by using the <code>Router<\/code> service and returning a <code>UrlTree<\/code> object as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"82\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-32.png\" alt=\"\" class=\"wp-image-687\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-32.png 831w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-32-300x30.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-32-768x76.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Note how the <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/the-inject-function\/\" target=\"_blank\" rel=\"noopener\" title=\"\">inject function<\/a> is used in all these guards to access any service we need.<\/p>\n\n\n\n<p><code>canActivate<\/code> works well when navigating to components. If we want to prevent the lazy-loading of a child route or prevent access to any path of the child route, then <code>canActivateChild<\/code> is what we need instead of <code>canActivate<\/code>.<\/p>\n\n\n\n<p>Finally, if you want to prevent the user from navigating away from the current path\/component, you can implement the <code>canDeactivate<\/code> guard. A typical example of such a use case is when working with complex forms where we want to make sure the user has saved their progress before navigating away from the current screen\/component:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-33.png\" alt=\"\" class=\"wp-image-689\" width=\"656\" height=\"33\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-33.png 656w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-33-300x15.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Another exciting feature is that we can access the current component itself instead of a service, which is especially useful for <code>canDeactivate<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"656\" height=\"76\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-34.png\" alt=\"\" class=\"wp-image-690\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-34.png 656w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-34-300x35.png 300w\" sizes=\"auto, (max-width: 656px) 100vw, 656px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If you like those new framework features, I covered some more <a href=\"https:\/\/blog.angulartraining.com\/router-utility-functions-in-angular-14-8d843b50d2e2\" target=\"_blank\" rel=\"noopener\" title=\"\">utility router functions released with Angular 14+ in this post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we introduced how route guards can control whether or not a user can navigate to a new URL in our application. The main guard to achieve that behavior is canActivate. This guard will run every time the user tries to access a path and only allow the user to proceed if the guard function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,25],"tags":[],"class_list":["post-685","post","type-post","status-publish","format-standard","hentry","category-angular","category-routing"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/685","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=685"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/685\/revisions"}],"predecessor-version":[{"id":694,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/685\/revisions\/694"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}