@Injectable()
export class APIService {
constructor(private http: Http) {
}
}
@Injectable()
export class CourseAPIService extends APIService{
constructor(private http2: Http){
super(http2);
}
}
不幸的是,我将基类作为 APIService,并将 Http 作为构造函数参数,我必须在子类 CourseAPIService 中再次传递它。 如果有任何解决方案,请帮助我......?
最佳答案
如果您没有在 CourseAPIService
中使用 private http2
属性,则可以从 CourseAPIService
中完全删除构造函数。
如果你想在CourseAPIService
中使用Http
服务,你可以将http
属性标记为protected
在 APIService
中并与 CourseAPIService
共享。
@Injectable()
export class APIService {
constructor(protected http: Http) {
}
}
但是,如果您为 CourseAPIService
定义构造函数,则必须使用 super
调用其基类构造函数并传递必要的参数。
关于angular - 如何避免在 TypeScript、Angular2 的派生类中重复基类构造函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43434471/