ShivamPatel
ShivamPatel

Reputation: 31

Why I am getting cyclic dependency issue when I upgrade to Angular 9

I have this file and its code is like

import { Injectable } from '@angular/core';
import { I18n } from '@ngx-translate/i18n-polyfill';
import { isNumber } from 'lodash';
import { ConfirmationService, MessageService } from 'primeng/api';

export enum ConfirmationType {
  Delete,
  Navigate,
  Unsaved,
}

export enum SuccessMessage {
  Cloned,
  Created,
  Deleted,
  Saved,
  Reverted,
}

@Injectable()
export class MessagingService {
  constructor(
    private readonly _i18n: I18n,
    // tslint:disable-next-line:ban-types
    private readonly _messageService: MessageService,
    private readonly _confirmationService: ConfirmationService,
  ) { }

And the error that I get is:

Error: Cannot instantiate cyclic dependency! MessagingService

Upvotes: -1

Views: 349

Answers (1)

Mahdi Zarei
Mahdi Zarei

Reputation: 7456

This error occurred because of that you instantiated the MessagingService inside of MessageService too.

This way you created a cycle of instantiation.

MessagingService -> MessageService -> MessagingService -> ...


You have to restruct your code in a way that they can access the required data and functions without need of instantiate each other.

Upvotes: 0

Related Questions