fix(core): require factory to be provided for shakeable InjectionToken (#22207)
InjectionToken can be created with an ngInjectableDef, and previously this allowed the full expressiveness of @Injectable. However, this requires a runtime reflection system in order to generate factories from expressed provider declarations. Instead, this change requires scoped InjectionTokens to provide the factory directly (likely using inject() for the arguments), bypassing the need for a reflection system. Fixes #22205 PR Close #22207
This commit is contained in:

committed by
Victor Berchet

parent
5dd2b5135d
commit
f755db78dc
@ -12,6 +12,16 @@ import {ServerModule} from '@angular/platform-server';
|
||||
|
||||
export interface IService { readonly data: string; }
|
||||
|
||||
@NgModule({})
|
||||
export class TokenModule {
|
||||
}
|
||||
|
||||
export const TOKEN = new InjectionToken('test', {
|
||||
scope: TokenModule,
|
||||
factory: () => new Service(),
|
||||
});
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'token-app',
|
||||
template: '{{data}}',
|
||||
@ -25,18 +35,12 @@ export class AppComponent {
|
||||
imports: [
|
||||
BrowserModule.withServerTransition({appId: 'id-app'}),
|
||||
ServerModule,
|
||||
TokenModule,
|
||||
],
|
||||
declarations: [AppComponent],
|
||||
bootstrap: [AppComponent],
|
||||
providers: [{provide: forwardRef(() => TOKEN), useClass: forwardRef(() => Service)}]
|
||||
})
|
||||
export class TokenAppModule {
|
||||
}
|
||||
|
||||
export class Service { readonly data = 'fromToken'; }
|
||||
|
||||
export const TOKEN = new InjectionToken('test', {
|
||||
scope: TokenAppModule,
|
||||
useClass: Service,
|
||||
deps: [],
|
||||
});
|
||||
export class Service { readonly data = 'fromToken'; }
|
Reference in New Issue
Block a user