我有以下课程:
export class CellLayer extends BaseLayer {
constructor(name: string, type: LayerType, private mapService: MapService) {
super(name, type, mapService);
}
}
和相应的抽象类:
export abstract class BaseLayer implements ILayer {
private _name: string;
private _type: LayerType;
constructor(name: string, type: LayerType, private mapService: MapService) {
this._name = name;
this._type = type;
}
}
应将全局MapService
对象传递给这两个类。
但是,我现在收到以下错误:
类型具有私有属性“mapService”的单独声明。 (6,14):Class'CellLayer'错误地扩展了基类'BaseLayer'。
答案 0 :(得分:6)
从private
构造函数中移除CellLayer
,并在protected
类中将其设为BaseLayer
。这样,您就可以访问mapService
课程中BaseLayer
的{{1}}成员。
CellLayer
答案 1 :(得分:5)
让它受到保护。
Private
表示该属性对当前类是私有的,因此子组件不能覆盖它,也不能定义它。
export abstract class BaseLayer implements ILayer {
private _name: string;
private _type: LayerType;
constructor(name: string, type: LayerType, protected mapService: MapService) {
this._name = name;
this._type = type;
}
}
export class CellLayer extends BaseLayer {
constructor(name: string, type: LayerType, protected mapService: MapService) {
super(name, type, mapService);
}
}