将角度服务传递给类和基类

use*_*875 3 angular

我有以下课程:

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'.

Mil*_*lad 6

保护它.

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)


zgu*_*gue 6

privateCellLayer构造函数中删除并protectedBaseLayer类中创建它.这样,您就可以访问mapService的成员BaseLayerCellLayer类.

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)