是否有可能有一个派生类,它的构造函数与其基类的参数不同?我在尝试拨打super
时遇到错误:
class Base {
constructor (private _myService) {}
}
class Derived extends Base {
constructor (private _myService, private _myOtherService) {
super(_myService);
}
}
Class'Derived'错误地扩展了基类'Base'。 类型具有私有属性“_myService”的单独声明。
class Derived extends Base {
constructor (_myService, private _myOtherService) {
super(_myService);
}
}
ERRORTS2341:属性'_myService'是私有的,只能在类'BaseClass'中访问。
class Derived extends Base {
constructor (private _myOtherService) {
super();
}
}
错误TS2554:预期1个参数,但得到0。
这样做的正确方法是什么?
答案 0 :(得分:1)
这样做的正确方法是什么?
不要在派生类中复制private
。修复示例:
export class Base {
constructor(private _myService: any) {
console.log(this._myService);
}
}
export class Derived extends Base {
constructor(_myService: any, private _myOtherService: any) {
super(_myService);
console.log(this._myOtherService)
}
}