简体   繁体   English

角子类成员未定义

[英]Angular subclass member undefined

I have an Angular component that contains some state information. 我有一个包含一些状态信息的Angular组件。 This component is a child component of another component that is used as a base class for inherited component classes. 该组件是另一个组件的子组件,该组件用作继承的组件类的基类。 This base class has a @ViewChild member containing the "state" component. 这个基类有一个@ViewChild成员,其中包含“状态”组件。 The base class has a method getState() that is used to fetch the inner state from the ViewChild. 基类具有getState()方法,该方法用于从ViewChild中获取内部状态。 This works fine when dealing with an actual base class component, but when it is an inherited class component, the @ViewChild member is undefined. 在处理实际的基类组件时,这很好用,但是当它是继承的类组件时, @ViewChild成员是未定义的。 I've read up on some of the unusual differences between inheritance in TypeScript and other OOP languages, but it didn't seem to answer this question. 我已经阅读了TypeScript和其他OOP语言之间的继承之间的一些不同寻常的区别,但是它似乎并没有回答这个问题。 I've also seen some issues reported that say that decorator properties (such as @Input , @Output , @ViewChild ) aren't inherited properly in Angular, and as a workaround you can just declare them again in the inherited class, but I tried this and it didn't resolve this issue. 我还看到一些问题报告说,装饰器属性(例如@Input@Output@ViewChild )在Angular中未正确继承,作为一种解决方法,您可以在继承的类中再次声明它们,但是我尝试了此操作,但无法解决此问题。

I've stripped down my code to the simplest case that shows the problem: 我将代码简化为最简单的情况来显示问题:

StateComponent : StateComponent

@Component({
  selector : 'app-state',
  template: `
  <p>Here's the state: {{state}}</p>
  `
})
export class StateComponent {
  state: string = "My state.";
}

BaseComponent : BaseComponent

@Component({
  selector : 'app-base',
  template: `
    <div>
      <p>BaseComponent</p>
      <app-state></app-state>
    </div>
    `
})
export class BaseComponent {
  @ViewChild(StateComponent) stateCmp: StateComponent;

  getState(): string {
    return this.stateCmp.state;
  }
}

InheritedComponent : InheritedComponent

@Component({
  selector : 'app-inherited',
  template: `
    <app-base></app-base>
    <div>
      <p>InheritedComponent</p>
    </div>
  `
})

export class InheritedComponent extends BaseComponent {
  constructor() {
    super();
  }
}

AppComponent : AppComponent

@Component({
  selector: 'app-root',
  template: `
  <app-inherited></app-inherited>
  <button (click)="onClick()">Get state</button>
  <p>{{ theState }}</p>
  `
})
export class AppComponent {
  @ViewChild(InheritedComponent) cmpRef: InheritedComponent;

  theState: string;

  onClick() {
    this.theState = this.cmpRef.getState();
  }
}

In this sample, clicking Get state fails because BaseComponent.stateCmp is undefined. 在此示例中,单击“ Get state失败,因为未定义BaseComponent.stateCmp。 Changing the AppComponent to use BaseComponent works fine and gets the state from the child component. 将AppComponent更改为使用BaseComponent可以正常工作,并从子组件获取状态。 Is there a way to do what I'm trying to do here? 有没有办法做我想在这里做的事情?

Here is a plunkr showing the failing case where AppComponent contains InheritedComponent. 这里是一个plunkr表示在AppComponent包含InheritedComponent失败的情况。

Here is a plunkr showing the working case where the only difference is AppComponent contains BaseComponent. 这是一个显示工作情况的插件 ,其中唯一的区别是AppComponent包含BaseComponent。

Okay, after thinking this through some more, I believe I've answered my own question. 好吧,经过更多思考之后,我相信我已经回答了我自己的问题。

The <app-base> in InheritedComponent is a different instance of BaseComponent . InheritedComponent<app-base>BaseComponent另一个实例

The InheritedComponent is-a BaseComponent , but since it doesn't have a StateComponent child directly, then it's cmpState is undefined. InheritedComponent是-A BaseComponent ,但由于它没有一个StateComponent直接孩子,那么它的cmpState是不确定的。

What I really needed here was a way to include the base component template in the inherited component's template, so that it was the actual base component itself, not a different instance. 我在这里真正需要的是一种将基本组件模板包括在继承组件的模板中的方法,以便它是实际的基本组件本身,而不是其他实例。 It looks like this is a proposed feature request in Angular: https://github.com/angular/angular/issues/13766 看来这是Angular中提出的功能请求: https : //github.com/angular/angular/issues/13766

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

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