填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

NG0201: No provider for {token} found!
没有找到此 {token} 的服务提供者!

说明

Description

尝试注入某个服务但未声明相应的提供者时,就会看到此错误。提供者是一种映射,它提供了一个值,你可以将该值注入到应用程序中类的构造函数中。

You see this error when you try to inject a service but have not declared a corresponding provider. A provider is a mapping that supplies a value that you can inject into the constructor of a class in your application.

在我们的“依赖注入”指南中阅读有关提供者的更多信息。

Read more on providers in our Dependency Injection guide.


如何排除本错误

Debugging the error

从本错误所指出的缺少提供者No provider for ${this}!)的对象开始进行工作。这通常会在服务中抛出,因为它请求了一个不存在的提供者。

Work backwards from the object where the error states that a provider is missing: No provider for ${this}!. This is commonly thrown in services, which require non-existing providers.

要修正此错误,以确保你的服务已注册在 NgModule 的提供者列表中或顶部带有 providedIn 属性的 @Injectable 装饰器中。

To fix the error ensure that your service is registered in the list of providers of an NgModule or has the @Injectable decorator with a providedIn property at top.

最常见的解决方案是在 @Injectable 中使用 providedIn 来添加提供者:

The most common solution is to add a provider in @Injectable using providedIn:

      
      @Injectable({ providedIn: 'app' })