Angular 6 - can't resolve all parameters for AppComponent

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

    You could try making the below changes:

    import {Component} from '@angular/core';
    import {TestService} from "./TestService";
    
    @Component({
      selector: 'sh-home',
      styleUrls: ['./home.scss'],
      templateUrl: './home.html',
      viewProviders: [TestService]
    })
    export class HomeComponent {
       constructor(private testService: TestService) {
         this.testService.sayHello();
       }
    }
    

    viewProviders creates a special injector that resolves dependencies only for this component.

提交回复
热议问题