我有以下类(class):

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 对象应该传递给两个类。

但是,我现在收到以下错误:

最佳答案

让它受到保护。
Private 表示该属性是当前类的私有(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);
    }
}

关于angular - 将 Angular 服务传递给类和基类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45980253/

10-09 18:10
查看更多