Currently, I have a component that needs to make an API request. In addition, there exists an abstract class designed for handling such services with the following constructor:
export class ODataService<T> extends ApiService {
constructor(http: HttpClient, api: string) {
super(http, null);
this.servicePath = `odata/${api}`;
}
}
This means the constructor is not empty. Instead of creating a separate service just for one method in one specific component, I am considering implementing a "create one-time use" service by using the abstract class solely for that component. Here's what I'm thinking:
@Component({
selector: 'my-component',
templateUrl: './my.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [{
provide: 'myService',
useFactory: (http) => new ODataService<IModel>(http, 'endpoint'),
deps: [HttpClient]
}]
})
export class MyComponent implements OnInit {
@Input() entityId: string
constructor(private myService: ODataService<IModel>) {
super();
}
}
The issue at hand is that it appears I cannot generate types dynamically and injecting the service in the component constructor leads to an error message:
This constructor was not compatible with Dependency Injection.
Hence, my question - is there a way to achieve this desired setup?