Angular 6 - can't resolve all parameters for AppComponent

前端 未结 10 871
时光取名叫无心
时光取名叫无心 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:22

    I also experienced the same issue with Angular 8.2.13 + Typescript. Finally the trick was to use a @Inject('RefOnlyUsedForTesting'), even if the type is string.

    export abstract class MyBaseClass {
      ....
      constructor(@Inject(ElementRef) elementRef: ElementRef,
              @Optional() @Inject('RefOnlyUsedForTesting') dep1: MyObject,
              @Optional() @Inject('RefOnlyUsedForTesting') dep2: string,
              @Optional() @Inject('RefOnlyUsedForTesting') dep3: string) {
      super();    
      ...
     }
    }
    
    0 讨论(0)
  • 2021-02-07 12:29

    I received the same error on Angular 8 app

    Can't resolve all parameters for AppComponent: (?,?,?)

    after changing 1 parameter type in the constructor().

    I solved the problem by stop and restarting the app.

    0 讨论(0)
  • 2021-02-07 12:30

    @Inject did the trick for me

    import {Component, Inject} from '@angular/core';
    import {TestService} from "./TestService";
    
    @Component({
      selector: 'sh-home',
      styleUrls: ['./home.scss'],
      templateUrl: './home.html'
    })
    export class HomeComponent {
    
      constructor(@Inject(TestService) testService: TestService) {
        testService.sayHello();
      }
    }
    
    0 讨论(0)
  • 2021-02-07 12:38

    Make sure you have following import in polyfills:

    import 'core-js/es7/reflect';
    
    0 讨论(0)
  • 2021-02-07 12:39

    If you are using webpack and babel to build Angular, it is likely you are missing this babel plugin: babel-plugin-transform-typescript-metadata

    0 讨论(0)
  • 2021-02-07 12:40

    I also got this issue but setting emitDecoratorMetadata": true in tsconfig solve the issue. Remember to restart the server.

    tsconfig.spec.json

    "compilerOptions": { "emitDecoratorMetadata": true, "outDir": "./out-tsc/spec",

    0 讨论(0)
提交回复
热议问题