Skip to content

Commit 7a6509b

Browse files
committed
refactor(core): NgModuleRef should not implement EnvironmentInjector interface (#46896)
This commit refactors the `NgModuleRef` implementation to drop functions required by the `EnvironmentInjector` interface. Previously the idea was that the `NgModuleRef` can act as an Injector to facilitate easier transition to standalone. However, from the mental model perspective, the `NgModuleRef` has the `injector` field, which is the correct injector reference and can be used is needed as an `EnvironmentInjector`. PR Close #46896
1 parent 422f06d commit 7a6509b

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

packages/core/src/render3/ng_module_ref.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ export function createNgModule<T>(
4444
* @deprecated Use `createNgModule` instead.
4545
*/
4646
export const createNgModuleRef = createNgModule;
47-
export class NgModuleRef<T> extends viewEngine_NgModuleRef<T> implements InternalNgModuleRef<T>,
48-
EnvironmentInjector {
47+
export class NgModuleRef<T> extends viewEngine_NgModuleRef<T> implements InternalNgModuleRef<T> {
4948
// tslint:disable-next-line:require-internal-with-underscore
5049
_bootstrapComponents: Type<any>[] = [];
5150
// tslint:disable-next-line:require-internal-with-underscore
5251
_r3Injector: R3Injector;
53-
override injector: EnvironmentInjector = this;
5452
override instance: T;
5553
destroyCbs: (() => void)[]|null = [];
5654

@@ -86,19 +84,11 @@ export class NgModuleRef<T> extends viewEngine_NgModuleRef<T> implements Interna
8684
// the module might be trying to use this ref in its constructor for DI which will cause a
8785
// circular error that will eventually error out, because the injector isn't created yet.
8886
this._r3Injector.resolveInjectorInitializers();
89-
this.instance = this.get(ngModuleType);
87+
this.instance = this._r3Injector.get(ngModuleType);
9088
}
9189

92-
get(token: any, notFoundValue: any = Injector.THROW_IF_NOT_FOUND,
93-
injectFlags: InjectFlags = InjectFlags.Default): any {
94-
if (token === Injector || token === viewEngine_NgModuleRef || token === INJECTOR) {
95-
return this;
96-
}
97-
return this._r3Injector.get(token, notFoundValue, injectFlags);
98-
}
99-
100-
runInContext<ReturnT>(fn: () => ReturnT): ReturnT {
101-
return this.injector.runInContext(fn);
90+
override get injector(): EnvironmentInjector {
91+
return this._r3Injector;
10292
}
10393

10494
override destroy(): void {

packages/core/test/acceptance/environment_injector_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ describe('environment injector', () => {
141141
expect(returnValue).toBe(3);
142142
});
143143

144+
it('should work with an NgModuleRef injector', () => {
145+
const ref = TestBed.inject(NgModuleRef);
146+
const returnValue = ref.injector.runInContext(() => 3);
147+
expect(returnValue).toBe(3);
148+
});
149+
150+
it('should return correct injector reference', () => {
151+
const ngModuleRef = TestBed.inject(NgModuleRef);
152+
const ref1 = ngModuleRef.injector.runInContext(() => inject(Injector));
153+
const ref2 = ngModuleRef.injector.get(Injector);
154+
expect(ref1).toBe(ref2);
155+
});
156+
144157
it('should make inject() available', () => {
145158
const TOKEN = new InjectionToken<string>('TOKEN');
146159
const injector = createEnvironmentInjector(

packages/router/test/router_preloader.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ describe('RouterPreloader', () => {
111111
const injector: any = getLoadedInjector(c[0]);
112112
const loadedRoutes: Route[] = getLoadedRoutes(c[0])!;
113113
expect(loadedRoutes[0].path).toEqual('LoadedModule1');
114-
expect(injector._parent).toBe((testModule as any)._r3Injector);
114+
expect(injector.parent).toBe((testModule as any)._r3Injector);
115115

116116
const injector2: any = getLoadedInjector(loadedRoutes[0]);
117117
const loadedRoutes2: Route[] = getLoadedRoutes(loadedRoutes[0])!;
118118
expect(loadedRoutes2[0].path).toEqual('LoadedModule2');
119-
expect(injector2._parent).toBe(injector);
119+
expect(injector2.parent).toBe(injector);
120120

121121
expect(events.map(e => e.toString())).toEqual([
122122
'RouteConfigLoadStart(path: lazy)',
@@ -213,11 +213,11 @@ describe('RouterPreloader', () => {
213213

214214
const injector = getLoadedInjector(c[0]) as any;
215215
const loadedRoutes = getLoadedRoutes(c[0])!;
216-
expect(injector._parent).toBe((testModule as any)._r3Injector);
216+
expect(injector.parent).toBe((testModule as any)._r3Injector);
217217

218218
const loadedRoutes2: Route[] = getLoadedRoutes(loadedRoutes[0])!;
219219
const injector3: any = getLoadedInjector(loadedRoutes2[0]);
220-
expect(injector3._parent).toBe(module2.injector);
220+
expect(injector3.parent).toBe(module2.injector);
221221
})));
222222
});
223223

0 commit comments

Comments
 (0)