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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49520047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:23:34  来源:igfitidea点击:

Types have separate declarations of a private property

javascripttypescript

提问by Mike Me

I am learning Angular (which is written in TypeScript) and I stumbled upon this error:

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

Class 'SnackbarService' incorrectly extends base class 'MatSnackBar'. Types have separate declarations of a private property '_overlay'.

“SnackbarService”类错误地扩展了基类“MatSnackBar”。类型具有私有属性“_overlay”的单独声明。

when trying to extend MatSnackBarfrom @angular/material.

当试图MatSnackBar@angular/material.

This is my code:

这是我的代码:

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.

任何类型的解释为什么会发生这种情况的任何帮助将不胜感激。

回答by Suren Srapyan

This happens because by declaring the constructor as taking a private _overlayparameter, you have created your own _overlay, but that is already defined in the base class MatSnackBar.

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

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

private从声明中删除该部分并从基类继承它。对其他构造函数参数执行相同操作。

export class SnackbarService extends MatSnackBar{

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

You can still access them via this.

您仍然可以通过以下方式访问它们 this.