{"id":4043,"date":"2016-12-08T11:14:28","date_gmt":"2016-12-08T09:14:28","guid":{"rendered":"https:\/\/adrhc.go.ro\/wordpress\/?p=4043"},"modified":"2017-05-09T09:40:32","modified_gmt":"2017-05-09T07:40:32","slug":"angular-2-router-guide","status":"publish","type":"post","link":"https:\/\/adrhc.go.ro\/blog\/angular-2-router-guide\/","title":{"rendered":"Comments on Angular 2 router guide"},"content":{"rendered":"<pre>\r\n<a href=\"https:\/\/angular.io\/docs\/ts\/latest\/guide\/router.html#!#resolve-guard\" target=\"_blank\">https:\/\/angular.io\/docs\/ts\/latest\/guide\/router.html#!#resolve-guard<\/a>\r\nCanDeactivateGuard is used in CrisisCenterRoutingModule which is part of CrisisCenterModule which is lazy loaded by AppRoutingModule (loadChildren: 'app\/crisis-center\/crisis-center.module#CrisisCenterModule').\r\n<strong>q:<\/strong> Why should CanDeactivateGuard be declared in AppRoutingModule providers? why not to CrisisCenterRoutingModule providers?\r\n<strong>guide answer:<\/strong> We also need to add the Guard to our main AppRoutingModule providers so the Router can inject it during the navigation process.\r\n<strong>my (original) comment<\/strong>\r\nAppRoutingModule seems to need to know in advance (at least for lazy loaded modules) the CanDeactivate guards used classes. It's odd why this does not apply for e.g. CrisisDetailResolve which is a service too and has the same configuration parent.\r\n<strong>the final answer<\/strong>\r\nWell, it seems that indeed CanDeactivateGuard could be provided by CrisisCenterRoutingModule too instead of AppRoutingModule. \r\nNow the question is why then the guide pointed AppRoutingModule as the one to provide CanDeactivateGuard?\r\nThe answer I guess is that the CanDeactivateGuard is a service useful to any module and because lazy loaded modules have their own root injector this means it would be created an additional instance of CanDeactivateGuard for any lazy load module that would provide it too. CanDeactivateGuard has no state so it's useless to have more than one instance of it.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/angular.io\/docs\/ts\/latest\/guide\/router.html#!#resolve-guard CanDeactivateGuard is used in CrisisCenterRoutingModule which is part of CrisisCenterModule which is lazy loaded by AppRoutingModule (loadChildren: &#8216;app\/crisis-center\/crisis-center.module#CrisisCenterModule&#8217;). q: Why should CanDeactivateGuard be declared in AppRoutingModule providers? why not to CrisisCenterRoutingModule providers? guide answer: We also need to add [&hellip;]<\/p>\n<div class=\"link-more\"><a href=\"https:\/\/adrhc.go.ro\/blog\/angular-2-router-guide\/#more-4043\" class=\"more-link\">Continue reading &#10142; <span class=\"screen-reader-text\">Comments on Angular 2 router guide<\/span><\/a><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,16,43,10],"tags":[],"class_list":["post-4043","post","type-post","status-publish","format-standard","hentry","category-angular","category-html-css-js","category-javascript","category-programming"],"_links":{"self":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts\/4043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/comments?post=4043"}],"version-history":[{"count":0,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/posts\/4043\/revisions"}],"wp:attachment":[{"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/media?parent=4043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/categories?post=4043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adrhc.go.ro\/blog\/wp-json\/wp\/v2\/tags?post=4043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}