Angular 6 providedin doesn't work -> StaticInjectorError


Angular 6 providedin doesn't work -> StaticInjectorError



I'm trying to use 'providedin' feature in Angular but receive the error "StaticInjectorError(AppModule)[DashboardComponent -> DashboardService]:"


@Injectable({
providedIn: DashboardModule
})
export class DashboardService {
getContent() {
return 'Dashboard Service works!!!';
}
}



Full demo https://stackblitz.com/edit/angular-kbkjet
Thanks for you effort!





providedIn does NOT automatically inject the service, you need to use the Injector to retrieve the service instance if you want to follow that specific route.
– briosheje
Jun 29 at 8:24


providedIn


Injector





@briosheje, do you mean this stackblitz.com/edit/angular-kbkjet? If not, could you provide an example?
– SIARHEI PAKHUTA
Jun 29 at 8:46






no, sorry, I didn't explain properly: if you don't use providedIn: 'root', the service won't be singleton. In your case, you can either use providedIn: 'root' or, in an easier way, add the DashboardService to the DashboardComponent providers.
– briosheje
Jun 29 at 8:49


providedIn: 'root'


providedIn: 'root'


DashboardService


DashboardComponent





@briosheje it will be a singleton for every dashboard module instance, which I suppose is the thing he wants. So how to make it tree-shakable on a non root level is the real question, because the @Injectable spec does allow other values than 'root'
– PierreDuc
Jun 29 at 8:54


@Injectable


'root'





@SIARHEIPAKHUTA this feels like an angular bug. You are working the exact same way as described here, which clearly states In the following excerpt, the @Injectable decorator is used to configure a provider that will be available in any injector that includes the HeroModule.
– PierreDuc
Jun 29 at 8:57



In the following excerpt, the @Injectable decorator is used to configure a provider that will be available in any injector that includes the HeroModule.




2 Answers
2



Use the providedIn: 'root'


providedIn: 'root'



When you use 'root', your injectable will be registered as a singleton in the application, and you don’t need to add it to the providers of the root module



Demo





Thanks, but I don't want to use it in a "root" level
– SIARHEI PAKHUTA
Jun 29 at 8:49






refer @briosheje comment
– Sachila Ranawaka
Jun 29 at 8:50



Finally I have found the solution: https://angular.io/guide/providers#providedin-and-ngmodules
Service also should be mentioned in the providers section of module.


@NgModule({
imports: [CommonModule],
declarations: [DashboardComponent],
exports: [DashboardComponent],
providers: [DashboardService]
})



Demo has been corrected.
Thank you guys!





Actually, the link you show are two ways to provide a service. The first way is failing for you for a weird reason. So yes, adding to providers work, but you lose the ability of tree shaking
– PierreDuc
Jun 29 at 8:59





@PierreDuc what do you mean "weird reason"? Could you provide an example how to use Service not in the root (application) level and keep ability of tree shaking? P.S. I have seen your post of original question. So, you think that this is an Angular bug. Maybe, but I can't believe that Google team is failed ))))
– SIARHEI PAKHUTA
Jun 29 at 9:15






By using providedIn: DashboardModule and not adding it to the providers array of the module. But somehow angular is being silly. Are you running the latest version, because this really feels like a bug
– PierreDuc
Jun 29 at 9:17


providedIn: DashboardModule





I use version 6.0.4 of Angular
– SIARHEI PAKHUTA
Jun 29 at 9:19





In demo code is used 6.0.7, it's the latest version
– SIARHEI PAKHUTA
Jun 29 at 9:22






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift