我有一个父服务,它有一些依赖,比如
@Injectable()
export class ParentService{
constructor(private http:Http, private customService:CustomService){}
}
我希望扩展服务
@Injectable()
export class ChildService extends ParentService{
constructor (){
super(??) <= typescript now asking to enter two parameters according to ParentServie's constructor
}
}
修改-----------------
@Injectable()
export class ParentService{
constructor(private http:Http, private customService:CustomService){}
get(){this.http.get(...)}
}
@Injectable()
export class ChildService extends ParentService{
constructor (private http:Http, private customService:CustomService){
super(http, customService)
}
}
然后我可以在组件中使用?
export class Cmp {
constructor(private childService:ChildService){
this.childService.get()
}
}
答案 0 :(得分:20)
需要重复超类的参数并传递给超级调用:
@Injectable()
export class ChildService extends ParentService{
constructor (http:Http, customService:CustomService){
super(http, customService);
}
}
有一些“黑客”可以解决,例如Inheritance and dependency injection
答案 1 :(得分:15)
简单地提供基本服务......非@Injectable()
!例如,您需要提供一种方法来检索LoginService
中的用户个人资料。这个方法对于LoginService
的不同实例会有所不同,而且父类不能知道这个方法来自何处:它可以是lambda,它可以是导出函数,它可以是另一个服务的方法。为此,您可以声明父服务
家长服务:
// Don't annotate it with @Injectable() ! It's a simple class.
export abstract class BaseLoginService {
constructor(
// This won't be injected automatically,
// child class must call super() and provide value for this argument.
// Note: it's private, can't be accessed outside of BaseLoginService class
private http: HttpClient,
// Method used to retrieve user profile
private profileGetter: () => Observable<UserDetails>,
private router: Router
) {
this.profileGetter().subscribe(); // Do something with this method
}
然后在子类中扩展它:
// Child class must be annotated with @Injectable(),
// if you don't need to extend it further
@Injectable()
export class LoginService extends BaseLoginService {
constructor(
// Note: no public/private/protected here - "http"
// will be just an argument
http: HttpClient,
private profileService: ProfileService, // This is the private field of LoginService class
router: Router,
// Some service used by LoginService class, parent class BaseLoginService
// doesn't need to know about SomeOtherService
private someOtherService: SomeOtherService
) {
super(
http,
// Need lambda here to capture profileService instance. If
// profileService.getUserDetailsMethod doesn't use instance
// fields or methods, then we can simply write
// "profileService.getUserDetailsMethod" (without quotes, obviously).
() => profileService.getUserDetailsMethod(),
router
);
this.someOtherService.doSomething(); // Custom initialization code
}
注意:在模块的providers
部分指定LoginService
而不是父BaseLoginService
:
providers: [
LoginService,
并在组件类中使用它:
export class LoginComponent implements OnInit {
constructor(
private loginService: LoginService
) {
}
如果您需要使用父服务(例如,在仅需要父服务类功能的共享组件中),请以这种方式提供BaseLoginService
:
providers: [
{provide: BaseLoginService, useExisting: LoginService}, // For those components which need functionality from base class only
LoginService, // Still need this one for those components which need functionality from child class
答案 2 :(得分:0)
如果您在抽象类protected
中进行DI服务,而在子类中省略了构造函数,则该服务在子类中可用。