Error: NG0203 when using inject() in abstract base class member
deKaantje opened this issue · comments
Stephan Kaneman commented
Is this a regression?
No
Description
I have a abstract base class with a member implementation that uses Angular 14+ inject() method to get the value for an injection token;
export abstract class SomeBaseService {
protected readonly someMember: MyType = inject(MY_TOKEN);
Injectable()
export class ConcreteService extends SomeBaseService {
}
This throws an error when using with Spectator :
const createService = createServiceFactory({
service: ConcreteService ,
providers: [{ provide: MY_TOKEN, useValue: myValue}],
});
beforeEach(() => (spectator = createService()));
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`. Find more at https://angular.io/errors/NG0203
error properties: Object({ code: -203 })
Error: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `EnvironmentInjector#runInContext`. Find more at https://angular.io/errors/NG0203
at injectInjectorOnly (node_modules/@angular/core/fesm2015/core.mjs:4754:15)
at ɵɵinject (node_modules/@angular/core/fesm2015/core.mjs:4765:60)
at inject (node_modules/@angular/core/fesm2015/core.mjs:4858:12)
at new SomeBaseService (path/to/lib/src/lib/services/base-service.ts:17:78)
at new ConcreteService (path/to/lib/src/lib/services/concrete-service.ts:17:5)
Please provide the environment you discovered this bug in
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 14.2.8
Node: 14.18.0
Package Manager: npm 6.14.15
OS: win32 x64
Angular: 14.2.9
... animations, common, compiler, compiler-cli, core, elements
... forms, platform-browser, platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.1402.8
@angular-devkit/build-angular 14.2.8
@angular-devkit/core 14.1.2
@angular-devkit/schematics 14.2.8
@angular/cdk 14.2.6
@angular/cli 14.2.8
@angular/material 14.2.6
@angular/material-moment-adapter 14.2.6
@schematics/angular 14.2.10
ng-packagr 14.2.2
rxjs 6.6.7
typescript 4.6.4
Anything else?
nope.
Do you want to create a pull request?
No
Netanel Basal commented
Does it work with plain Angular?