1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Angular 6 Services: providedIn: 'root' vs CoreModule

Discussão em 'Angular' iniciado por Manish Jain, Setembro 25, 2024.

  1. Manish Jain

    Manish Jain Guest

    With Angular 6, below is the preferred way to create singleton services:

    import { Injectable } from '@angular/core';

    @Injectable({
    providedIn: 'root',
    })
    export class UserService {
    }


    From Angular doc: When you provide the service at the root level, Angular creates a single, shared instance of HeroService and injects into any class that asks for it. Registering the provider in the @Injectable metadata also allows Angular to optimize an app by removing the service if it turns out not to be used after all.

    Also,

    providers: [
    // no need to place any providers due to the `providedIn` flag...
    ]


    So, does that mean we no more need CoreModule? We can import services and other common modules directly into AppModule.

    Continue reading...

Compartilhe esta Página