Circular dependency in DI detected for ApplicationRef. How to fix it
Asked Answered
O

5

9

I am a beginner in Angular and tried to implement global error handling in the application for this purpose I create appErrorHandler class and implement ErrorHandler and I inject Toast Service on this class but it shows me above error. AppErrorHandler class.

  @Injectable()
 export class AppErrorHandler implements ErrorHandler {

constructor(private toastService: ToastrService) {}

handleError(error: any): void {
    this.toastService.error("An unexpected error","Error");
}    

}

AppModule

 providers: [
{ provide: ErrorHandler, useClass: AppErrorHandler },
MakeService
],
 imports: [
BrowserAnimationsModule,
  ToastrModule.forRoot(
  {
    timeOut: 6000,
   positionClass: 'toast-bottom-right',
   preventDuplicates: true,
  }
),
RouterModule.forRoot([
  
  {  path:'', component:HomeComponent },
  {  path:'vehicle-form', component:VehicleFormComponent },
  
]),

],

Error image

Oversold answered 9/1, 2021 at 16:46 Comment(4)
aren't you missing {providedIn: 'root'} in the AppErrorHandler's @Injectable decorator?Calces
it does not make a differenceOversold
I see... is there any chance you could replicate the thing in stackblitz? that would really be helpful :)Calces
To handle this scenario, you can make use of 2 approaches - Make use of HttpInterceptor eg: Example-HttpInterceptor - Creating Factor Method eg: Resolve-Circular-DependencyHexameter
M
16

I had a similar issue, and didn't find the real cause. Anyway, this can be fixed by hack (source: https://github.com/ngneat/hot-toast/issues/70)

In your case:

@Injectable()
export class AppErrorHandler implements ErrorHandler {

constructor(@Inject(Injector) private readonly injector: Injector) {}

private get toastService() {
    return this.injector.get(ToastrService);
}

handleError(error: any): void {
    this.toastService.error("An unexpected error","Error");
}
Maximilien answered 30/7, 2021 at 15:22 Comment(0)
L
0

In my case the custom error handler was receiving dependencies, which themselves needed something that wasn't injected explicitly in the module.

    {
      provide: ErrorHandler,
      useClass: GlobalErrorHandler,
      deps: [
        BugReporterService, // This needed more dependencies
        NgZone,
        NgxUiDialogService,
        MessageService,
        TranslateService,
      ],
    },

What's counterintuitive is that it doesn't tell you which dependency is missing, so my suggestion is to divide-et-impera by commenting out dependencies until you find the culprit. Recursively.

Lemmie answered 16/9, 2023 at 10:6 Comment(0)
S
0

I was getting this error after i upgraded angular to 15 .. was getting this error only while running ng test so i cleared this issue by removing browser animation module in spec file from imports and I have added shared module at the top.. always check the order of your imports ..first shared module and then browser module should be in app module..and add browser animation module at the last of your core module imports..

Sheryl answered 21/12, 2023 at 8:29 Comment(0)
C
0

--NPM error message indicates a mismatch or missing peer dependency for @angular/animations when trying

solution :

  1. npm install @angular/[email protected]
  2. npm cache clean --force
  3. npm install
Clementia answered 18/7 at 13:4 Comment(0)
W
0

In my case, I had this rare Angular runtime circular dependency in DI, which was not reported by the compiler.

After trying all suggestions, I had to:

1 - Rename .Angular folder to .Angular.old;

2 - npm install

3 - ng serve

I also switched .angular.old <=> .angular folders, followed by steps 2 and 3 a couple times, for consistency on my conclusion.

Sometimes, it's not our codebase fault ;-)

Warr answered 13/9 at 21:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.