angular injectable chain stop working when moved into a library

Discussão em 'Angular' iniciado por Mako, Setembro 13, 2024.

  1. Mako

    Mako Guest

    In the following angular app named "app-standalone" I chained the injectables CoreService, OuterService, OutmostService and used them in the AppComponent (https://github.com/moky80/angular-i...aster/app-standalone/src/app/app.component.ts) 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” (https://github.com/moky80/angular-i...projects/lib-core/src/lib/lib-core.service.ts):

    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” (https://github.com/moky80/angular-i...er/app-uses-lib-core/src/app/app.component.ts) 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?

    Continue reading...

Compartilhe esta Página