NullInjectorError: No provider for ActivatedRoute!




Asked on May 26, 2023
I my Angular standalone application, I have an error.

How to fix it.

ERROR NullInjectorError: R3InjectorError(Standalone[MainAppComponent])[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: 
  NullInjectorError: No provider for ActivatedRoute!



Replied on May 26, 2023
Use provideRouter to configure routes.

Try

bootstrapApplication(MainAppComponent, {
    providers: [
        ------
        provideRouter(ROUTES),
    ]
}); 




Replied on August 21, 2023

The error message "NullInjectorError: No provider for ActivatedRoute!" means that the Angular injector cannot find a provider for the ActivatedRoute service. This can happen if the ActivatedRoute service is not imported in your application, or if it is not provided in the injector.

To fix this error, you need to make sure that the ActivatedRoute service is imported in your application and that it is provided in the injector.

Here are the steps on how to fix the error:

  1. Import the ActivatedRoute service in your application.
import { ActivatedRoute } from '@angular/router';
  1. Provide the ActivatedRoute service in the injector.
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    RouterModule.forRoot([
      { path: '', component: AppComponent }
    ])
  ],
  providers: [ActivatedRoute]
})
export class AppModule { }
  1. Reload your application.

Once you have done these steps, the error should be fixed.

Here are some other things to check if you are still getting the error:

  • Make sure that you are using the latest version of Angular.
  • Make sure that the ActivatedRoute service is not being imported from a different library.
  • Make sure that the ActivatedRoute service is not being provided by a different module.


Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us