类型有单独的私有属性声明

我正在学习 Angular (用 TypeScript 编写) ,偶然发现了这个错误:

类“ SnackbarService”错误地扩展了基类“ MatSnackBar”。 Types have separate declarations of a private property '_overlay'.

当试图从 @angular/material延伸 MatSnackBar时。

这是我的暗号:

import { MatSnackBar } from '@angular/material';
import { Overlay } from '@angular/cdk/overlay';
import { LiveAnnouncer } from '@angular/cdk/a11y';
...


export class SnackbarService extends MatSnackBar {


constructor(
private _overlay: Overlay,
private _liveAnnouncer: LiveAnnouncer,
...
) {
super(_overlay, _liveAnnouncer, ...);
}
}
}

Any help with any type of explanation on why this happens would be really be appreciated.

82983 次浏览

这是因为通过将构造函数声明为接受 private _overlay参数,您已经创建了自己的 _overlay,但是这已经在基类 MatSnackBar中定义了。

Remove the private part from the declaration and inherit it from the base class. Do the same for the other constructor parameters.

export class SnackbarService extends MatSnackBar{


constructor(
_overlay: Overlay,
_liveAnnouncer: LiveAnnouncer,
...
) {
super(_overlay, _liveAnnouncer, ...);
}
}
}

你仍然可以通过 this.访问它们

在依赖关系版本不匹配的情况下也会发生这种情况

例如,如果您的应用程序 A使用包 B的版本1.0和包 C的版本1.0。然而,软件包 C 使用了软件包 B的不同版本,比如2.0。

现在,在整个构建过程中有两个名称相同的不同类。 要解决这个问题,你必须升级软件包 C 或者升级软件包 B,这样才能在应用程序中使用相同的版本。