Angular 6 - can't resolve all parameters for AppComponent

前端 未结 10 907
时光取名叫无心
时光取名叫无心 2021-02-07 11:59

I am trying to build an application with Angular 6 and I am still setting everything up. But it seems there is something wrong with the dependency injection in my app.

10条回答
  •  攒了一身酷
    2021-02-07 12:43

    I had the same issue with a component in Angular 8. The constructor had multiple services as parameters.

    e.g.,

    constructor(
      private router: Router,
      private fooService: FooService,
      private barService: BarService,
      private fubarService: FoobarService,
      private anotherService: AnotherService)
    {}
    

    Experimenting with @Inject(), some parameters will take it, but others will not. They will complain about the parameter type.

    The common thing about FooService, BarService and FubarService was they were physically in the same directory. I created some subdirectories and put one service in each sub-directory -- and the compiler error went away.

    A blog post said forwardRef worked for them. It did not work for me. However, their article provided insight into what might be going on.

    Edited to add:

    In two other cases, the above wouldn't work. In one case, changing the import of the service from full path (src/app/...) to relative path removed the compiler complaint (go figure). In the second case, adding @Inject(ServiceName) public service: ServiceName resolved the issue.

    In all of the above cases, an Ionic 5 project that uses Angular had no compilation complaints. The Ionic project is using Angular 8.2.14. The Angular project is using 8.2.0 (defaults of the respective CLIs when I created the projects).

    Looks like some bug is lurking somewhere...

提交回复
热议问题