'No provider' error (even though I've added a provider)

后端 未结 3 1176
走了就别回头了
走了就别回头了 2021-01-20 18:18

I\'m trying to call a service from a particular class, but for some reason it can\'t be called, even though I am able to use it in other classes. I\'m using @Input

相关标签:
3条回答
  • 2021-01-20 18:38

    In my case, I imported the service as

    import {MyService} from '../services/my.service'
    

    in one place, and

    import {MyService} from '../services/index'
    

    in the other place, while index.ts contains all export statements for services in the same folder.

    After I unified to the 2nd form of importing, the problem had gone.

    0 讨论(0)
  • 2021-01-20 18:39

    This will happen if something wrong was provided as a provider. And it is unlikely that there may be another reason for this.

    In one place it is ...shared/service/cert.service, and in another it is ...shared/Service/cert.service. Case matters. It depends on the environment what happens with these modules during build process, but this can cause errors, e.g. they may become duplicated and refer to different objects.

    To quickly debug this, CertService can be exposed from both files as window.CertService1 and window.CertService2 and compared in console if both are defined and equal.

    0 讨论(0)
  • 2021-01-20 18:40

    Exactly the same error message ("No provider for SomeService") can be triggered by having another service with the same name in your app. So it would look like you correctly included SomeService #1 in your module, added to the providers section, but it would still throw you the error. It puzzled me until I realized that there's SomeService #2 being imported in one of my components.

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