如何在角度2中扩展具有依赖性的服务

Han*_*Che 26 extends typescript angular2-services angular

我有一个父服务,有一些依赖,如

@Injectable()
export class ParentService{
  constructor(private http:Http, private customService:CustomService){}
}
Run Code Online (Sandbox Code Playgroud)

我想扩展服务

@Injectable()
export class ChildService extends ParentService{
  constructor (){
    super(??) <= typescript now asking to enter two parameters according to ParentServie's constructor
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑 - - - - - - - - -

@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)
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我可以在组件中使用?

export class Cmp {
  constructor(private childService:ChildService){
    this.childService.get()
  }
}
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 22

需要重复超类的参数并传递给超级调用:

@Injectable()
export class ChildService extends ParentService{
  constructor (http:Http, customService:CustomService){
    super(http, customService);
  }
}
Run Code Online (Sandbox Code Playgroud)

继承和依赖注入一样,有一些"黑客"可以解决


izo*_*fif 17

简单地做基础服务......非@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
  }
Run Code Online (Sandbox Code Playgroud)

然后在子类中扩展它:

// 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
  }
Run Code Online (Sandbox Code Playgroud)

注意:在providers模块的部分指定LoginService而不是父BaseLoginService:

providers: [
  LoginService,
Run Code Online (Sandbox Code Playgroud)

并在组件类中使用它:

export class LoginComponent implements OnInit {
  constructor(
    private loginService: LoginService
  ) {
  }
Run Code Online (Sandbox Code Playgroud)

如果您需要使用父服务(例如,在仅需要来自父服务类的功能的共享组件中),则提供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
Run Code Online (Sandbox Code Playgroud)


Joe*_*ugh 5

如果在抽象类中创建 DI 服务protected并在子类中省略构造函数,则这些服务在子类中可用。