Error when trying to inject a service into an angular component “EXCEPTION: Can't resolve all parameters for component”, why?

前端 未结 30 1975
无人共我
无人共我 2020-11-22 17:18

I\'ve built a basic app in Angular, but I have encountered a strange issue where I cannot inject a service into one of my components. It injects fine into any of the three o

30条回答
  •  面向向阳花
    2020-11-22 17:49

    for angular 6 and newer versions, try

    @Injectable({
      providedIn: 'root'
    })
    

    ..right above your service class with no other lines in between

    advantages

    • no need to add the service to any module (will be "auto-discovered")
    • service will be a singleton (since it will be injected into root)

    [angular docs]

提交回复
热议问题