@@ -13,6 +13,7 @@ import {
1313 EnvironmentInjector ,
1414 inject ,
1515 Injectable ,
16+ Injector ,
1617 Type ,
1718} from '@angular/core' ;
1819
@@ -34,25 +35,28 @@ const appsWithLoaders = new WeakMap<
3435 */
3536@Injectable ( { providedIn : 'root' } )
3637export class _CdkPrivateStyleLoader {
37- private _appRef = inject ( ApplicationRef ) ;
38+ private _appRef : ApplicationRef | undefined ;
39+ private _injector = inject ( Injector ) ;
3840 private _environmentInjector = inject ( EnvironmentInjector ) ;
3941
4042 /**
4143 * Loads a set of styles.
4244 * @param loader Component which will be instantiated to load the styles.
4345 */
4446 load ( loader : Type < unknown > ) : void {
45- let data = appsWithLoaders . get ( this . _appRef ) ;
47+ // Resolve the app ref lazily to avoid circular dependency errors if this is called too early.
48+ const appRef = ( this . _appRef = this . _appRef || this . _injector . get ( ApplicationRef ) ) ;
49+ let data = appsWithLoaders . get ( appRef ) ;
4650
4751 // If we haven't loaded for this app before, we have to initialize it.
4852 if ( ! data ) {
4953 data = { loaders : new Set ( ) , refs : [ ] } ;
50- appsWithLoaders . set ( this . _appRef , data ) ;
54+ appsWithLoaders . set ( appRef , data ) ;
5155
5256 // When the app is destroyed, we need to clean up all the related loaders.
53- this . _appRef . onDestroy ( ( ) => {
54- appsWithLoaders . get ( this . _appRef ) ?. refs . forEach ( ref => ref . destroy ( ) ) ;
55- appsWithLoaders . delete ( this . _appRef ) ;
57+ appRef . onDestroy ( ( ) => {
58+ appsWithLoaders . get ( appRef ) ?. refs . forEach ( ref => ref . destroy ( ) ) ;
59+ appsWithLoaders . delete ( appRef ) ;
5660 } ) ;
5761 }
5862
0 commit comments