Uncaught (in promise): Error: StaticInjectorError(AppModule)[options]

后端 未结 8 1655
借酒劲吻你
借酒劲吻你 2020-12-01 14:08

I have a strange error. Usually (I did my googling), in this case of errors Angular specifies in square brackets which exactly module/service/provider/etc caused the problem

相关标签:
8条回答
  • 2020-12-01 14:28

    Here is what worked for me (Angular 7):

    First import HttpClientModule in your app.module.ts if you didn't:

    import { HttpClientModule } from '@angular/common/http';
    ...
    imports: [
            HttpClientModule
        ],
    

    Then change your service

    @Injectable()
    export class FooService {
    

    to

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

    Hope it helps.

    Edit:

    providedIn

    Determines which injectors will provide the injectable, by either associating it with an @NgModule or other InjectorType, or by specifying that this injectable should be provided in one of the following injectors:

    'root' : The application-level injector in most apps.

    'platform' : A special singleton platform injector shared by all applications on the page.

    'any' : Provides a unique instance in every module (including lazy modules) that injects the token.

    Be careful platform is available only since Angular 9 (https://blog.angular.io/version-9-of-angular-now-available-project-ivy-has-arrived-23c97b63cfa3)

    Read more about Injectable here: https://angular.io/api/core/Injectable

    0 讨论(0)
  • 2020-12-01 14:36

    It also might be that you haven't declared you Dependency Injected service, as a provider in the component that you injected it to. That was my case :)

    0 讨论(0)
提交回复
热议问题