when I execute my angular application locally, this error occurred suddenly even without doing any code changes while the same code base can be run in a separate machine with the same configuration.
- node version:v16.17.0
ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: NullInjectorError: No provider for ActivatedRoute!
when I execute my angular application locally, this error occurred suddenly even without doing any code changes while the same code base can be run in a separate machine with the same configuration.
- node version:v16.17.0
Share Improve this question asked Mar 20, 2023 at 7:42 Pubudu Mahesh MeththanandaPubudu Mahesh Meththananda 3976 silver badges17 bronze badgesERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[ActivatedRoute -> ActivatedRoute -> ActivatedRoute]: NullInjectorError: No provider for ActivatedRoute!
2 Answers
Reset to default 5Try to add the following import in AppModule file
imports: [
RouterModule.forRoot([]),
...
],
To import RouterModule you should do something like this:
import { RouterModule } from '@angular/router';
My friend removed provideRouter(routes),
from appConfig when he added provideHttpClient()
Fixed this error by adding it back
// app.config.ts (Angular 18.x)
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideRouter(routes), // <-- added back
]
};