[英]How to avoid repeating base class constructor parameters in derived class in TypeScript, Angular2?
@Injectable()
export class APIService {
constructor(private http: Http) {
}
}
@Injectable()
export class CourseAPIService extends APIService{
constructor(private http2: Http){
super(http2);
}
}
I have baseclass as APIService having Http as constructor parameter unfortunately I have to pass it again in child class CourseAPIService. 不幸的是,我将基类作为APIService,并将Http作为构造函数参数,但我必须在子类CourseAPIService中再次传递它。 Please help me if any soln ...?
请帮助我...
You can remove the constructor completely from CourseAPIService
if you are not using the private http2
property in CourseAPIService
. 您可以删除完全从构造
CourseAPIService
,如果你不使用private http2
财产CourseAPIService
。
If you do want to use the Http
service in CourseAPIService
, you can mark the http
property as protected
in APIService
and share it with CourseAPIService
. 如果确实要在
CourseAPIService
使用Http
服务, CourseAPIService
可以将http
属性标记为在APIService
protected
,并与CourseAPIService
共享。
@Injectable()
export class APIService {
constructor(protected http: Http) {
}
}
However if you define a constructor for CourseAPIService
you must call its base class constructor with super
passing necessary arguments. 但是,如果为
CourseAPIService
定义构造函数, CourseAPIService
必须使用super
传递必要的参数来调用其基类构造函数。
AFAIK, the answer is No. You need call super() from derived class. AFAIK,答案是否定的。您需要从派生类调用super()。 So, if your base class has dependencies, your derived class must inject same dependencies and pass to super() method.
因此,如果您的基类具有依赖项,则派生类必须注入相同的依赖项并传递给super()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.