Skip to content

ERROR Error: inject() must be called from an injection context when using inheritance in a library #35277

@sherif-elmetainy

Description

@sherif-elmetainy

🐞 bug report

Affected Package

@angular/core

Is this a regression?

Yes. Worked in version 8

Description

In a minimal angular app, with a minimal angular library. The library has a pipe that inherits from an abstract class which also has a constructor that injects a service.

import { Injectable, PipeTransform, Pipe } from '@angular/core';

@Injectable({
    providedIn: 'root',
})
export class SayHelloService {
    public sayHello(name: any): string {
        return `Say ${name}.`;
    }
}

@Injectable()
export abstract class ParentPipe {
    constructor(
        protected readonly sayHelloService: SayHelloService
    ) {

    }
}

@Pipe({
    name: 'sayHello',
})
export class SayHelloPipe extends ParentPipe implements PipeTransform {
    transform(value: any, ...args: any[]) {
        return this.sayHelloService.sayHello(value);
    }
}

When I build the library, and try to use the pipe it in the app. I get an "Error: inject() must be called from an injection context" (exception details below).

I tried to move the code from my library, to the main app, same error. Then I tried to add a @Pipe dectorator to the ParentPipe class.

@Pipe({
    name: ''
})
export abstract class ParentPipe {
    constructor(
        protected readonly sayHelloService: SayHelloService
    ) {
    }
}

It worked in the app, but when I tried to move the code back to the library with --prod option I got the error


ERROR: Cannot determine the module for class ParentPipe in D:/Temp/testapp/projects/testlib/src/lib/say-hello.pipe.ts! Add ParentPipe to the NgModule to fix it.

An unhandled exception occurred: Cannot determine the module for class ParentPipe in D:/Temp/testapp/projects/testlib/src/lib/say-hello.pipe.ts! Add ParentPipe to the NgModule to fix it.
See "C:\Users\sherif\AppData\Local\Temp\3\ng-NIqaLb\angular-errors.log" for further details.

This scenario was working well in version 8, 7, etc. Previously the @Injectable on the parent abstract class with constructor injecting service was enough. Now it is broken for abstract classes, blocking the migration of a library I have to support version 9.

Now I can refactor the code, and make the parent class not abstract, add the parent class in the module without exporting it, but is this the way to do that? And does that mean that inheriting from an abstract class is not supported in version 9?

🔬 Minimal Reproduction

https://github.com/sherif-elmetainy/testapp

Clone the repo
run:


yarn
yarn build testlib --prod
yarn start

🔥 Exception or Error


core.js:5828 ERROR Error: inject() must be called from an injection context
    at injectInjectorOnly (core.js:899)
    at Module.ɵɵinject (core.js:915)
    at ParentPipe_Factory (testlib.js:17)
    at SayHelloPipe_Factory (testlib.js:27)
    at Module.ɵɵpipe (core.js:36106)
    at AppComponent_Template (app.component.ts:13)
    at executeTemplate (core.js:11841)
    at renderView (core.js:11627)
    at renderComponent (core.js:13143)
    at renderChildComponents (core.js:11430)
defaultErrorLogger @ core.js:5828
main.ts:12 Error: inject() must be called from an injection context
    at injectInjectorOnly (core.js:899)
    at Module.ɵɵinject (core.js:915)
    at ParentPipe_Factory (testlib.js:17)
    at SayHelloPipe_Factory (testlib.js:27)
    at Module.ɵɵpipe (core.js:36106)
    at AppComponent_Template (app.component.ts:13)
    at executeTemplate (core.js:11841)
    at renderView (core.js:11627)
    at renderComponent (core.js:13143)
    at renderChildComponents (core.js:11430)

🌍 Your Environment

Angular Version:


@angular-devkit/architect          0.900.1
@angular-devkit/build-angular      0.900.1
@angular-devkit/build-ng-packagr   0.900.1
@angular-devkit/build-optimizer    0.900.1
@angular-devkit/build-webpack      0.900.1
@angular-devkit/core               9.0.1
@angular-devkit/schematics         9.0.1
@angular/cli                       9.0.1
@ngtools/webpack                   9.0.1
@schematics/angular                9.0.1
@schematics/update                 0.900.1
ng-packagr                         9.0.0
rxjs                               6.5.4
typescript                         3.7.5
webpack                            4.41.2

Anything else relevant?

Metadata

Metadata

Assignees

Labels

area: coreIssues related to the framework runtimeregressionIndicates than the issue relates to something that worked in a previous versionstate: has PRtype: bug/fix

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions