我有以下课程:
export class CellLayer extends BaseLayer {
constructor(name: string, type: LayerType, private mapService: MapService) {
super(name, type, mapService);
}
}
Run Code Online (Sandbox Code Playgroud)
和相应的抽象类:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
全局MapService对象应该传递给这两个类.
但是,我现在收到以下错误:
类型具有私有属性"mapService"的单独声明.(6,14):Class'CellLayer'错误地扩展了基类'BaseLayer'.
保护它.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
private从CellLayer构造函数中删除并protected在BaseLayer类中创建它.这样,您就可以访问mapService的成员BaseLayer在CellLayer类.
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, mapService: MapService) {
super(name, type, mapService);
}
}
Run Code Online (Sandbox Code Playgroud)