{"id":695,"date":"2023-03-23T05:00:00","date_gmt":"2023-03-23T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=695"},"modified":"2023-03-22T15:20:35","modified_gmt":"2023-03-22T22:20:35","slug":"canmatch-router-guard","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/canmatch-router-guard\/","title":{"rendered":"canMatch Router Guard"},"content":{"rendered":"\n<p>After introducing <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-use-angular-route-guards\/\" target=\"_blank\" rel=\"noopener\" title=\"\">guards<\/a> and the most common <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/route-guards-canactivate-canactivatechild-candeactivate\/\" target=\"_blank\" rel=\"noopener\" title=\"\">guard functions (<code>canActivate<\/code>, <code>canActivateChild<\/code>, and <code>canDeactivate<\/code>)<\/a>, we will cover one slightly different guard that can run when trying to access a route.<\/p>\n\n\n\n<p><code><a href=\"https:\/\/angular.io\/api\/router\/CanMatchFn#description\" target=\"_blank\" rel=\"noopener\" title=\"\">canMatch<\/a><\/code> was introduced in <a href=\"https:\/\/blog.angulartraining.com\/whats-new-in-angular-15-1-fbd1cb5abcab\" target=\"_blank\" rel=\"noopener\" title=\"\">Angular 15.1<\/a>. When <code>canMatch<\/code> returns <code>false<\/code>,  Angular will continue looking at other routes that would match that path instead of preventing any navigation.<\/p>\n\n\n\n<p>Let&#8217;s consider the following example:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"609\" height=\"256\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-35.png\" alt=\"\" class=\"wp-image-697\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-35.png 609w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/03\/image-35-300x126.png 300w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>With the above code, both routes are configured for the <code>\/user<\/code> path, but if the user is an admin user, they&#8217;ll land on an <code>AdminDetails<\/code> component, and if they&#8217;re a regular user, they&#8217;ll land on a <code>UserDetails<\/code> component. <\/p>\n\n\n\n<p>This approach can be helpful when working with multiple user roles that display different components.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After introducing guards and the most common guard functions (canActivate, canActivateChild, and canDeactivate), we will cover one slightly different guard that can run when trying to access a route. canMatch was introduced in Angular 15.1. When canMatch returns false, Angular will continue looking at other routes that would match that path instead of preventing any [&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-695","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\/695","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=695"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":707,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/695\/revisions\/707"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}