你可以使用Angular的Dependency Injection系统.您可以try 使用工厂函数来提供服务.
@Injectable()
export class CalculateFieldsService {
constructor(
private jsonForm: UntypedFormGroup,
private destroy: DestroyRef
) {
// Service constructor
}
// Rest of the service code...
}
export function CalculateFieldsServiceFactory(jsonForm: UntypedFormGroup, destroy: DestroyRef) {
return new CalculateFieldsService(jsonForm, destroy);
}
在组件中,您可以配置组件修饰器中的提供程序,以使用工厂函数提供依赖项和服务.
import { Component, Injectable, Inject } from '@angular/core';
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
providers: [
{
provide: CalculateFieldsService,
useFactory: CalculateFieldsServiceFactory,
deps: [UntypedFormGroup, DestroyRef]
}
]
})
export class YourComponent {
constructor(private calculateFieldsService: CalculateFieldsService) {
// You can now use the service with the provided dependencies.
}
}