Search code examples
angulartypescriptsonarqube

Angular - Sonar - Constructor has too many parameters (8). Maximum allowed is 7


enter image description here

My angular app showing above error message when it pass thought sonar scan. Is there any way to fixed it.

I want to pass 8 or 9 dependency but sonar lint not allowing. Is there any alternate way to make it possible?


Solution

  • You can use dynamic service injection in the component. In that case it will have one parameter and you can inject as many service as you want.

    import { Component, Injector } from '@angular/core';
    import { MyService } from './my.service';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      name = 'Angular';
      myService : MyService;
    
      constructor(private injector : Injector){
          this.myService = injector.get<MyService>(MyService);
     }
    }