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

前端 未结 30 1846
无人共我
无人共我 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:43

    WRONG #1: Forgetting Decorator:

    //Uncaught Error: Can't resolve all parameters for MyFooService: (?).
    export class MyFooService { ... }
    

    WRONG #2: Omitting "@" Symbol:

    //Uncaught Error: Can't resolve all parameters for MyFooService: (?).
    Injectable()
    export class MyFooService { ... }
    

    WRONG #3: Omitting "()" Symbols:

    //Uncaught Error: Can't resolve all parameters for TypeDecorator: (?).
    @Injectable
    export class MyFooService { ... }
    

    WRONG #4: Lowercase "i":

    //Uncaught ReferenceError: injectable is not defined
    @injectable
    export class MyFooService { ... }
    

    WRONG #5: You forgot: import { Injectable } from '@angular/core';

    //Uncaught ReferenceError: Injectable is not defined
    @Injectable
    export class MyFooService { ... }
    

    CORRECT:

    @Injectable()
    export class MyFooService { ... }
    

提交回复
热议问题