📌  相关文章
📜  NullInjectorError:没有诊断提供程序! (1)

📅  最后修改于: 2023-12-03 15:33:13.779000             🧑  作者: Mango

NullInjectorError: No provider for {{dependency}}!

Introduction

This error occurs in Angular applications when a required dependency is not provided by the application's injector.

In simple terms, the injector is responsible for creating and providing instances of services, components, and other dependencies throughout the application. If a dependency is not provided, the application cannot function properly.

Cause

This error is caused by a missing or incorrectly specified provider in the application's module or component.

Solution

To resolve this issue, you need to ensure that the required provider is correctly specified in the application's module or component. This can be done by adding the required provider to the providers array in the module or component decorator.

For example, if the error message is "No provider for HttpClient!", you can fix it by adding the HttpClientModule to the providers array in the app.module.ts file as follows:

import { HttpClientModule } from '@angular/common/http';

@NgModule({
  imports: [ HttpClientModule ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Conclusion

By ensuring that all dependencies are correctly provided, you can avoid the "NullInjectorError: No provider for {{dependency}}!" error and ensure that your Angular application functions correctly.