66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { Injector , ProviderToken , ɵisInjectable as isInjectable } from '@angular/core' ;
10-
9+ import {
10+ Injector ,
11+ ProviderToken ,
12+ ɵisInjectable as isInjectable ,
13+ EnvironmentInjector ,
14+ runInInjectionContext ,
15+ } from '@angular/core' ;
1116import { RunGuardsAndResolvers } from '../models' ;
17+ import { getClosestRouteInjector } from './config' ;
18+
1219import { ChildrenOutletContexts , OutletContext } from '../router_outlet_context' ;
1320import {
1421 ActivatedRouteSnapshot ,
@@ -42,11 +49,18 @@ export function getAllRouteGuards(
4249 future : RouterStateSnapshot ,
4350 curr : RouterStateSnapshot ,
4451 parentContexts : ChildrenOutletContexts ,
52+ environmentInjector : EnvironmentInjector ,
4553) : Checks {
4654 const futureRoot = future . _root ;
4755 const currRoot = curr ? curr . _root : null ;
4856
49- return getChildRouteGuards ( futureRoot , currRoot , parentContexts , [ futureRoot . value ] ) ;
57+ return getChildRouteGuards (
58+ futureRoot ,
59+ currRoot ,
60+ parentContexts ,
61+ [ futureRoot . value ] ,
62+ environmentInjector ,
63+ ) ;
5064}
5165
5266export function getCanActivateChild (
@@ -80,6 +94,7 @@ function getChildRouteGuards(
8094 currNode : TreeNode < ActivatedRouteSnapshot > | null ,
8195 contexts : ChildrenOutletContexts | null ,
8296 futurePath : ActivatedRouteSnapshot [ ] ,
97+ environmentInjector : EnvironmentInjector ,
8398 checks : Checks = {
8499 canDeactivateChecks : [ ] ,
85100 canActivateChecks : [ ] ,
@@ -89,7 +104,14 @@ function getChildRouteGuards(
89104
90105 // Process the children of the future route
91106 futureNode . children . forEach ( ( c ) => {
92- getRouteGuards ( c , prevChildren [ c . value . outlet ] , contexts , futurePath . concat ( [ c . value ] ) , checks ) ;
107+ getRouteGuards (
108+ c ,
109+ prevChildren [ c . value . outlet ] ,
110+ contexts ,
111+ futurePath . concat ( [ c . value ] ) ,
112+ environmentInjector ,
113+ checks ,
114+ ) ;
93115 delete prevChildren [ c . value . outlet ] ;
94116 } ) ;
95117
@@ -106,6 +128,7 @@ function getRouteGuards(
106128 currNode : TreeNode < ActivatedRouteSnapshot > ,
107129 parentContexts : ChildrenOutletContexts | null ,
108130 futurePath : ActivatedRouteSnapshot [ ] ,
131+ environmentInjector : EnvironmentInjector ,
109132 checks : Checks = {
110133 canDeactivateChecks : [ ] ,
111134 canActivateChecks : [ ] ,
@@ -121,6 +144,7 @@ function getRouteGuards(
121144 curr ,
122145 future ,
123146 future . routeConfig ! . runGuardsAndResolvers ,
147+ environmentInjector ,
124148 ) ;
125149 if ( shouldRun ) {
126150 checks . canActivateChecks . push ( new CanActivate ( futurePath ) ) ;
@@ -137,12 +161,20 @@ function getRouteGuards(
137161 currNode ,
138162 context ? context . children : null ,
139163 futurePath ,
164+ environmentInjector ,
140165 checks ,
141166 ) ;
142167
143168 // if we have a componentless route, we recurse but keep the same outlet map.
144169 } else {
145- getChildRouteGuards ( futureNode , currNode , parentContexts , futurePath , checks ) ;
170+ getChildRouteGuards (
171+ futureNode ,
172+ currNode ,
173+ parentContexts ,
174+ futurePath ,
175+ environmentInjector ,
176+ checks ,
177+ ) ;
146178 }
147179
148180 if ( shouldRun && context && context . outlet && context . outlet . isActivated ) {
@@ -156,11 +188,25 @@ function getRouteGuards(
156188 checks . canActivateChecks . push ( new CanActivate ( futurePath ) ) ;
157189 // If we have a component, we need to go through an outlet.
158190 if ( future . component ) {
159- getChildRouteGuards ( futureNode , null , context ? context . children : null , futurePath , checks ) ;
191+ getChildRouteGuards (
192+ futureNode ,
193+ null ,
194+ context ? context . children : null ,
195+ futurePath ,
196+ environmentInjector ,
197+ checks ,
198+ ) ;
160199
161200 // if we have a componentless route, we recurse but keep the same outlet map.
162201 } else {
163- getChildRouteGuards ( futureNode , null , parentContexts , futurePath , checks ) ;
202+ getChildRouteGuards (
203+ futureNode ,
204+ null ,
205+ parentContexts ,
206+ futurePath ,
207+ environmentInjector ,
208+ checks ,
209+ ) ;
164210 }
165211 }
166212
@@ -171,9 +217,11 @@ function shouldRunGuardsAndResolvers(
171217 curr : ActivatedRouteSnapshot ,
172218 future : ActivatedRouteSnapshot ,
173219 mode : RunGuardsAndResolvers | undefined ,
220+ environmentInjector : EnvironmentInjector ,
174221) : boolean {
175222 if ( typeof mode === 'function' ) {
176- return mode ( curr , future ) ;
223+ const injector = getClosestRouteInjector ( future ) ?? environmentInjector ;
224+ return runInInjectionContext ( injector , ( ) => mode ( curr , future ) ) ;
177225 }
178226 switch ( mode ) {
179227 case 'pathParamsChange' :
0 commit comments