Reputation: 2445
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
Upvotes: 0
Views: 1040
Reputation: 19843
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'
})
Upvotes: 1