简体   繁体   English

为家长班注入服务

[英]Injecting service to parent class

Is it possible to inject services into parent class? 是否可以将服务注入父类? I've got two classes. 我有两节课。 One parent class and ex. 一个家长班和一个。 IncomeService which extends parent. 扩展父级的IncomeService。

Parent: 家长:

@Injectable()
export class ObjectService {
    constructor(protected apiService: ApiService,
                protected cacheService: CacheService) {
    }
}

IncomeService IncomeService

@Injectable()
export class IncomeService extends ObjectService {
    test() {
        this.apiService; //is undefined
    }
}

I want to inject servcies to parent since every child will use those services and are used in parent class. 我想向父母注入服务,因为每个孩子都会使用这些服务,并在父母课堂中使用。 As I see angular is not injecting apiService and cacheService to IncomeService. 如我所见, cacheService没有将apiServicecacheService注入IncomeService。 Is it a feature or a bug? 是功能还是错误?

There is basically no other way than to repeat the constructor parameters in the subclass and pass it to the superclass using the super(...) call. 除了在子类中重复构造函数参数,然后使用super(...)调用将其传递给超类,基本上没有其他方法。

@Injectable()
export class IncomeService extends ObjectService {
    constructor(protected apiService: ApiService,
                protected cacheService: CacheService) {
      super(apiService, cacheService);
    }
    test() {
        this.apiService; //is undefined
    }
}

There are answers with some ugly hacks to work around this but I'd discourage. 有一些丑陋的骇客可以解决此问题,但我不建议这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM