简体   繁体   English

无法从派生类访问/获取抽象(基)类属性的值 - TypeScript

[英]Unable to access/get value of abstract (base) class properties from derived class - TypeScript

I have 2 classes - one is abstract and another class is extending it.我有 2 个类 - 一个是抽象的,另一个类正在扩展它。 In ABSTRACT class I have some public/protected properties which ARE initialized in constructor.在 ABSTRACT 类中,我有一些在构造函数中初始化的公共/受保护属性。 Let it be abstract Parent and Child extends Parent让它是抽象的 Parent 和 Child 扩展 Parent

Questions:问题:

  1. Why, when I am trying to get value of the properties of abstract class like: super.somePropertyOfParent it is always UNDEFINED, but when I call it like: this.somePropertyOfParent it HAS value?为什么,当我试图获取抽象类的属性值时:super.somePropertyOfParent 它总是未定义的,但是当我这样称呼它时:this.somePropertyOfParent 它有值? Logically, super constructor is always called first, so these fields should be initialized first of all.从逻辑上讲,超级构造函数总是首先被调用,所以这些字段应该首先被初始化。

  2. I have 2 BehaviourSubjects (countryValue, languageValue) in my Parent abstract class, which are initialized with some 'initial value' in constructor.我的 Parent 抽象类中有 2 个 BehaviourSubjects (countryValue, languageValue),它们在构造函数中用一些“初始值”进行了初始化。 In Child class in OnInit method (which obviously called after Parent constructor) I am subscribing to Parent's BehaviourSubjects like: this.countryValue.subscribe(...) and it receives the 'INITIAL' value.在 OnInit 方法的 Child 类中(显然在 Parent 构造函数之后调用),我订阅了 Parent 的 BehaviourSubjects,例如:this.countryValue.subscribe(...) 并且它接收到“INITIAL”值。 Then in Parent's class ngOnChange method calls subject.next(...), but Child doesn't receive new value...why?然后在Parent类的ngOnChange方法调用subject.next(...),但是Child没有收到新值...为什么?

PS if make BehaviourSubject properties STATIC and refer to the ClassName.property - everything works fine. PS 如果将 BehaviourSubject 属性设为 STATIC 并参考 ClassName.property - 一切正常。

Please see code below:请看下面的代码:

@Directive()
export abstract class IbCustomElementComponent implements OnChanges{

  @Input('data-country') country = '';
  @Input('data-language') language = '';

  public countryValue:BehaviorSubject<string>;
  public languageValue:BehaviorSubject<string>;

 

  protected constructor(public translateService: TranslateService) {
    this.countryValue = new BehaviorSubject<string>('initial');
    this.languageValue = new BehaviorSubject<string>('initial');
  }

  abstract customElementReady(changes: SimpleChanges): void;

  ngOnChanges(changes: SimpleChanges) {

    if (this.country && this.language) {
      this.translateService.use(this.country.toLocaleLowerCase() + '-' + this.language);
      this.customElementReady(changes);
      this.countryValue.next(this.country);
      this.languageValue.next(this.language);
    }
  }
}

export class CustomerCardsComponent extends IbCustomElementComponent implements OnInit {


  displayedColumns: string[] = ['fieldName', 'value'];

  CARD_DATA: CardData[][] = [];

  dataSource = this.CARD_DATA;

  cards: Card[] = [];

  currentCustomer : Customer = new Customer();


  constructor(private customerListService: CustomerListService, public override translateService: TranslateService) {
    super(translateService);
  }

  ngOnInit(): void {
    this.countryValue.subscribe(c=>{
      this.currentCustomer.bic = Bic[c.toUpperCase()];
      if(this.currentCustomer.bic){
        this.getCustomerCards(this.currentCustomer)
      }
    })
  }
}

Firstly, the reason you can't call super.somePropertyOfParent is because your abstract class isn't initialised separately from your derived class — your derived class simply inherits all of the properties from the abstract class.首先,你不能调用super.somePropertyOfParent的原因是你的抽象类没有与你的派生类分开初始化——你的派生类只是继承了抽象类的所有属性。 That is why you call this and not super .这就是为什么你this而不是super

For the ngOnChanges side of things, I believe what's happening is your abstract class' method isn't called because, as far as I understand, Angular components/directives are annoying when it comes to inherited lifecycle hooks.对于ngOnChanges方面,我相信正在发生的事情是没有调用您的抽象类的方法,因为据我了解,Angular 组件/指令在继承的生命周期挂钩方面很烦人。 I know I've had issues with OnInit and OnDestroy in the past.我知道我过去曾遇到过OnInitOnDestroy的问题。

I would try implementing OnChanges in your derived class as follows:我会尝试在您的派生类中实现OnChanges ,如下所示:

ngOnChanges(changes: SimpleChanges) {
  super.ngOnChanges(changes);
}

Thus you only use super when referring to the parent class' implementation of a method, and not for any properties which your child class automatically inherits.因此,只有在引用父类的方法实现时才使用super ,而不是子类自动继承的任何属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM