1

I tried to reproduce the problem in a stackblitz, but I do not have the warning in the blitz..

Basically,

my-module.module.ts import a component, so depend on it

import { MyComponentComponent } from './my-component/my-component.component';

the component, use a service

import { MyServiceService } from '../my-service.service';

the service has

@Injectable({
  providedIn: MyModuleModule
})

so depend on the module

import { MyModuleModule } from './my-module.module';

it looks circular to me, but where in my app I have warning, I do not have in stackblitz.

Is this correct implementation?

A fix would be remove the

@Injectable({
  providedIn: MyModuleModule
})

But does this make my service provided in every module? or none of them?

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
Crocsx
  • 2,534
  • 1
  • 28
  • 50

1 Answers1

2

Just make the service

providedIn: 'root'

A service needs to be provided somewhere or else the dependency injection doesn't know where to get an instance from. Getting rid of the providedIn: MyModuleModule will error when you add it to a constructor list of dependencies. providedIn: 'root' means it doesn't need to be added to any modules.

Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
  • Oh ok, so providedIn: 'root' will not add the module anywhere unless I specify it as provider ? – Crocsx Apr 24 '19 at 03:40