gpt4 book ai didi

angular - 将 Angular 服务传递给类和基类

转载 作者:太空狗 更新时间:2023-10-29 18:04:38 27 4
gpt4 key购买 nike

我有以下类(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 对象应该传递给两个类。

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

Types have separate declarations of a private property 'mapService'. (6,14): Class 'CellLayer' incorrectly extends base class 'BaseLayer'.

最佳答案

让它受到保护。

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com