fix(core): don't override ngInjectableDef in the decorator if present on the type (#22943)
Previously, @Injectable() would generate an ngInjectableDef on the type it was decorating, even if that type already had a compiled ngInjectableDef, overwriting the compiled version. PR Close #22943
This commit is contained in:
parent
4f0cae0676
commit
6f0191744c
|
@ -124,4 +124,22 @@ describe('ngInjectableDef Bazel Integration', () => {
|
||||||
|
|
||||||
expect(TestBed.get(Service).value).toEqual('overridden');
|
expect(TestBed.get(Service).value).toEqual('overridden');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('does not override existing ngInjectableDef', () => {
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
useValue: new Service(false),
|
||||||
|
})
|
||||||
|
class Service {
|
||||||
|
constructor(public value: boolean) {}
|
||||||
|
static ngInjectableDef = {
|
||||||
|
providedIn: 'root',
|
||||||
|
factory: () => new Service(true),
|
||||||
|
token: Service,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
expect(TestBed.get(Service).value).toEqual(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -119,7 +119,8 @@ export const Injectable: InjectableDecorator = makeDecorator(
|
||||||
'Injectable', undefined, undefined, undefined,
|
'Injectable', undefined, undefined, undefined,
|
||||||
(injectableType: InjectableType<any>,
|
(injectableType: InjectableType<any>,
|
||||||
options: {providedIn?: Type<any>| 'root' | null} & InjectableProvider) => {
|
options: {providedIn?: Type<any>| 'root' | null} & InjectableProvider) => {
|
||||||
if (options && options.providedIn !== undefined) {
|
if (options && options.providedIn !== undefined &&
|
||||||
|
injectableType.ngInjectableDef === undefined) {
|
||||||
injectableType.ngInjectableDef = defineInjectable({
|
injectableType.ngInjectableDef = defineInjectable({
|
||||||
providedIn: options.providedIn,
|
providedIn: options.providedIn,
|
||||||
factory: convertInjectableProviderToFactory(injectableType, options)
|
factory: convertInjectableProviderToFactory(injectableType, options)
|
||||||
|
|
Loading…
Reference in New Issue