Dependency injection, Angular

0

I am learning Angular and caught a little in dependency injections. On the website it says that the decorator @Injectable () must have a service provider declared. Something like this:

@Injectable ({   providedIn: 'root', }) But when I put this in the IDE accuses error, saying that the decorator expects the arguments and has a gift, and the project does not work. How do I resolve this?

    
asked by anonymous 09.05.2018 / 20:25

1 answer

1

So put

@Injectable() 
export class MeuService 

and put the service inside the providers in app.module

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { JokeModule } from './joke/joke.module';
import { MeuService } from './meuService.service';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule       
  ],
  providers: [ MeuService ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Placing the service in the app module will be a singleton for the entire application that in most cases and the desired behavior.

    
10.05.2018 / 09:52