From 7cb8639da9ca484ac823fc4a68d373ff25252971 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Fri, 28 Mar 2025 18:05:51 -0700 Subject: [PATCH] refactor(core): add `ApplicationRef.prototype.bootstrapImpl` with an `injector` parameter (#60622) This allows any components individually bootstrapped to inherit from a unique `Injector`. This is useful when bootstrapping multiple root components with different providers. For now, the function is private while we explore potential designs to consolidate it with the existing `ApplicationRef.prototype.bootstrap` method. PR Close #60622 --- .../core/src/application/application_ref.ts | 10 ++++++- packages/core/test/application_ref_spec.ts | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/core/src/application/application_ref.ts b/packages/core/src/application/application_ref.ts index 0edff39546c..7ef3493d26a 100644 --- a/packages/core/src/application/application_ref.ts +++ b/packages/core/src/application/application_ref.ts @@ -498,6 +498,14 @@ export class ApplicationRef { bootstrap( componentOrFactory: ComponentFactory | Type, rootSelectorOrNode?: string | any, + ): ComponentRef { + return this.bootstrapImpl(componentOrFactory, rootSelectorOrNode); + } + + private bootstrapImpl( + componentOrFactory: ComponentFactory | Type, + rootSelectorOrNode?: string | any, + injector: Injector = Injector.NULL, ): ComponentRef { profiler(ProfilerEvent.BootstrapComponentStart); @@ -532,7 +540,7 @@ export class ApplicationRef { ? undefined : this._injector.get(NgModuleRef); const selectorOrNode = rootSelectorOrNode || componentFactory.selector; - const compRef = componentFactory.create(Injector.NULL, [], selectorOrNode, ngModule); + const compRef = componentFactory.create(injector, [], selectorOrNode, ngModule); const nativeElement = compRef.location.nativeElement; const testability = compRef.injector.get(TESTABILITY, null); testability?.registerApplication(nativeElement); diff --git a/packages/core/test/application_ref_spec.ts b/packages/core/test/application_ref_spec.ts index d559774b24a..f96b1295d2a 100644 --- a/packages/core/test/application_ref_spec.ts +++ b/packages/core/test/application_ref_spec.ts @@ -256,6 +256,34 @@ describe('bootstrap', () => { ), ); }); + + describe('bootstrapImpl', () => { + it('should use a provided injector', inject([ApplicationRef], (ref: ApplicationRef) => { + class MyService {} + const myService = new MyService(); + + @Component({ + selector: 'injecting-component', + template: `
Hello, World!
`, + }) + class InjectingComponent { + constructor(readonly myService: MyService) {} + } + + const injector = Injector.create({ + providers: [{provide: MyService, useValue: myService}], + }); + + createRootEl('injecting-component'); + const appRef = ref as unknown as {bootstrapImpl: ApplicationRef['bootstrapImpl']}; + const compRef = appRef.bootstrapImpl( + InjectingComponent, + /* rootSelectorOrNode */ undefined, + injector, + ); + expect(compRef.instance.myService).toBe(myService); + })); + }); }); describe('destroy', () => {