Angular 6 StaticInjectorError: No provider for Options

假如想象 提交于 2019-12-05 03:33:48

This might be late for OP but I was able to solve this issue by:

Adding the necessary service to my providers list under the app.module.ts

Hope this helps those in the future who are about to encounter this issue.

Can you try changing the import of SimpleNotificationsModule as SimpleNotificationsModule.forRoot() I was running into the same error and changing it this way fixed it for me.

Can the problem be that you have a non static service? If you have a non static service you must specifiy it as a provider in the Component that uses it. It is not enough to specify it as a provider in the app.modules.ts

Non static service

Remove the injectable directive in the service (Usually looks like this)

@Injectable({
   providedIn: 'root'
})

In the Component that uses the non static service

Add the providers attribute and specify the service. You need to import it too, just as you have to do with regular services as well.

import { EntryQueueHub } from 'app/services/hubs/entry-queue.hub';
@Component({
  selector: 'app-image-viewer',
  templateUrl: './image-viewer.component.html',
  styleUrls: ['./image-viewer.component.scss'],
  providers: [EntryQueueHub]
})
...
  constructor (
    private entryQueueHub: EntryQueueHub,
    ...

Missing some modules with static methods to configure the provider can also make same kind of issue.

For example consider, RouterTestingModule, it has a static method called withRoutes() to configure the provider.

From angular's compiler's and injector's point of view this can be seen as a missing token in the injector.

You have to pay attention in order to import all necessary modules/services that your app needs. In my case, I forgot to include SimpleNotificationsModule.forRoot() in the app.module (imports section) becuase I was using Angular2NotificationsService.

Adding the service under providers of app.module does solves the problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!