0

I am trying to inject a service into a component in my Angular app. I am using version 7 of Angular.

Here is my dashboard.component:

import { ArtistService } from './artist.service';

export class AdminDashboardComponent implements OnInit {
    constructor(private _artistService: ArtistService) { }
}

Here is some of my artist.service.ts file:

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

@Injectable()
    export class ArtistService {
}

When I navigate to the dashboard component, this error is logged to the console:

Error: Uncaught (in promise):

Error: StaticInjectorError(AppModule)[AdminDashboardComponent -> ArtistService]:

StaticInjectorError(Platform: core)[AdminDashboardComponent -> ArtistService]: NullInjectorError: No provider for ArtistService!

I've tried to resolve this by updating my service with this decorator:

@Injectable({
    providedIn: 'root'
})

But I still receive the same error. Can someone please point out what I need to change? Thanks a lot in advance

user9847788
  • 2,135
  • 5
  • 31
  • 79

1 Answers1

1

either you need to provide your service in a module (AppModule for example) like below

@NgModule({
   // ... other codes 
   providers: [
      ArtistService
      // ... other codes
   ]
})

or as you mentioned using providedIn as below in your service

@Injectable({
    providedIn: 'root'
})
Reza
  • 18,865
  • 13
  • 88
  • 163