填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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' })