In the following angular app named “app-standalone” I chained the injectables CoreService, OuterService, OutmostService and used them in the AppComponent (angular-injectable-chain/app-standalone/src/app/app.component.ts at master · moky80/angular-injectable-chain · GitHub) and they worked as expected:
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { Injectable } from '@angular/core';
@Injectable({ providedIn: 'root' })
export class CoreService {
constructor() { console.log("Initialized Core");
} }
@Injectable({ providedIn: 'root' })
export class OuterService {
constructor(private core: CoreService) {
console.log("Initialized Outer");
}}
@Injectable({ providedIn: 'root' })
export class OutmostService { constructor(private outer: OuterService) {
console.log("Initialized Outmost");
}}
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'app-standalone';
constructor(private outmost: OutmostService){
console.log("Initialized AppComponent");
}
}
In the next step, I moved CoreService, OuterService into the angular library named “lib-core” (angular-injectable-chain/lib-core/projects/lib-core/src/lib/lib-core.service.ts at master · moky80/angular-injectable-chain · GitHub):
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class CoreService {
constructor() {
console.log("Initialized Core");
}
}
@Injectable({
providedIn: 'root'
})
export class OuterService {
constructor(private core: CoreService) {
console.log("Initialized Outer");
}
}
and created a new app named “app-uses-lib-core” (angular-injectable-chain/app-uses-lib-core/src/app/app.component.ts at master · moky80/angular-injectable-chain · GitHub) which uses the lib-core:
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterOutlet } from '@angular/router';
import { Injectable } from '@angular/core';
import { OuterService } from 'lib-core';
@Injectable({ providedIn: 'root' })
export class OutmostService { constructor(private outer: OuterService) {
console.log("Initialized Outmost");
}}
@Component({
selector: 'app-root',
standalone: true,
imports: [CommonModule, RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'app-uses-lib-core';
constructor(private outmost: OutmostService){
console.log("Initialized AppComponent");
}
}
“app-uses-lib-core” got the run-time 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 runInInjectionContext
.”
I couldn’t see why moving the CoreService and OuterService into lib-core could cause an issue. Does somebody have an idea what is wrong with “lib-core” and “app-uses-lib-core” and how can I fix it provided that angular 17 is used?