1

Since updating to Angular 8.1 (8.0 worked), I'm getting a circular dependency warning that doesn't really make sense to me.

I have an Injectable Service SelectService that is provided in some components. The service is using a class SelectableItem. When creating an instance of this class, the service itself is passed as an argument.

item = new SelectableItem(
   ...
   this
);

The SelectableItemclass is not Injectable and looks like this.

export class SelectableItem {
    constructor(..., public selectService: SelectService) { }
}

Now since the 8.1 update i get a circular dependecy warning:

selectable-item.ts -> select.service.ts -> selectable-item.ts

SelectableItem is not a service, how can it be a circular dependency?

Thomas Schneiter
  • 1,103
  • 2
  • 19
  • 35

1 Answers1

0

Your SelectService class should look something like this. With the dependency to SelectableItem as a property of the service class.

import "SelectAbleItem" from "../selectable-item.ts

@Injectable()
class SelectService {
}

private selectItem: SelectItem;
public get SelectItem():SelectItem { return this.selectItem; }

Now in the constructor of the SelectableItem don't inject the SelectService but rather use the injector to provide it.

class SelectableItem {
  constructor() {
    const injector = Injector.create([{provide: SelectService, useClass: SelectService, deps: [SelectableItem]]};
    const selectService = injector.get(SelectService);
  }
}

I got some code from here and here. The code isn't tested and may contain a small glitch but it should point you in the right direction.

Youp Bernoulli
  • 5,303
  • 5
  • 39
  • 59
  • I still get the same error when using the Injector. The code is [here](https://github.com/Crazyht/ngx-tree-select/tree/dev/src/ngx-tree-select/src), it's not my repo i'm just trying to fix it. – Thomas Schneiter Jul 22 '19 at 09:33