From 69448a4178304cc35c12ca4537045099d14453c9 Mon Sep 17 00:00:00 2001 From: vthinkxie Date: Tue, 13 Apr 2021 10:10:35 +0800 Subject: [PATCH] feat(core): support flags option in NodeInjector.get close #31776 the InectFlags was defined in both getOrCreateInjectable and Injector, but was ignored in NodeInjector this PR support getting parent token via injector.get in NodeInjector --- packages/core/src/di/injector.ts | 2 +- packages/core/src/render3/di.ts | 4 +-- packages/core/test/acceptance/di_spec.ts | 40 +++++++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/core/src/di/injector.ts b/packages/core/src/di/injector.ts index a299d9a66410d2..ce999ec8efb3c8 100644 --- a/packages/core/src/di/injector.ts +++ b/packages/core/src/di/injector.ts @@ -71,7 +71,7 @@ export abstract class Injector { * @deprecated from v4.0.0 use ProviderToken * @suppress {duplicate} */ - abstract get(token: any, notFoundValue?: any): any; + abstract get(token: any, notFoundValue?: any, flags?: InjectFlags): any; /** * @deprecated from v5 use the new signature Injector.create(options) diff --git a/packages/core/src/render3/di.ts b/packages/core/src/render3/di.ts index 352d4baf801630..87cea749c2d011 100644 --- a/packages/core/src/render3/di.ts +++ b/packages/core/src/render3/di.ts @@ -688,8 +688,8 @@ export class NodeInjector implements Injector { private _tNode: TElementNode|TContainerNode|TElementContainerNode|null, private _lView: LView) {} - get(token: any, notFoundValue?: any): any { - return getOrCreateInjectable(this._tNode, this._lView, token, undefined, notFoundValue); + get(token: any, notFoundValue?: any, flags?: InjectFlags): any { + return getOrCreateInjectable(this._tNode, this._lView, token, flags, notFoundValue); } } diff --git a/packages/core/test/acceptance/di_spec.ts b/packages/core/test/acceptance/di_spec.ts index 69cda5ce1bac90..4b9a5cfa2f0062 100644 --- a/packages/core/test/acceptance/di_spec.ts +++ b/packages/core/test/acceptance/di_spec.ts @@ -7,7 +7,7 @@ */ import {CommonModule} from '@angular/common'; -import {Attribute, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, ContentChild, Directive, ElementRef, EventEmitter, forwardRef, Host, HostBinding, Inject, Injectable, InjectionToken, INJECTOR, Injector, Input, LOCALE_ID, NgModule, NgZone, Optional, Output, Pipe, PipeTransform, Self, SkipSelf, TemplateRef, ViewChild, ViewContainerRef, ViewRef, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core'; +import {Attribute, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, ContentChild, Directive, ElementRef, EventEmitter, forwardRef, Host, HostBinding, Inject, Injectable, InjectFlags, InjectionToken, INJECTOR, Injector, Input, LOCALE_ID, NgModule, NgZone, Optional, Output, Pipe, PipeTransform, Self, SkipSelf, TemplateRef, ViewChild, ViewContainerRef, ViewRef, ɵDEFAULT_LOCALE_ID as DEFAULT_LOCALE_ID} from '@angular/core'; import {ɵINJECTOR_SCOPE} from '@angular/core/src/core'; import {ViewRef as ViewRefInternal} from '@angular/core/src/render3/view_ref'; import {TestBed} from '@angular/core/testing'; @@ -1919,6 +1919,44 @@ describe('di', () => { }); }); }); + + onlyInIvy('Ivy support InjectFlags in Injector') + .it('should support SkipSelf flag in Injector', () => { + @Component({ + selector: 'parent', + template: '', + providers: [{ + provide: 'token', + useValue: 'PARENT', + }] + }) + class ParentComponent { + } + + @Component({ + selector: 'child', + template: '...', + providers: [{ + provide: 'token', + useValue: 'CHILD', + }] + }) + class ChildComponent { + constructor(public injector: Injector, @SkipSelf() public parentInjector: Injector) {} + } + + TestBed.configureTestingModule({ + declarations: [ParentComponent, ChildComponent], + }); + const fixture = TestBed.createComponent(ParentComponent); + fixture.detectChanges(); + + const childComponent = + fixture.debugElement.query(By.directive(ChildComponent)).componentInstance; + expect(childComponent.injector.get('token')).toBe('CHILD'); + expect(childComponent.injector.get('token', null, InjectFlags.SkipSelf)).toBe('PARENT'); + expect(childComponent.parentInjector.get('token')).toBe('PARENT'); + }); }); });