简体   繁体   English

无法访问传递给Angular组件的@Input字段的对象中的公共方法

[英]Can't access public method in an object passed to an Angular component's @Input field

I've declared the following configurator object. 我已经声明了以下配置器对象。

export class Config {
  constructor(public index: number, public junk: string[] = []) { }
  public count() : number { return this.junk.length; }
}

Then I pass it into my components input decorated field like this ( @Input() config : Config; ). 然后我将它传递给我的组件输入装饰字段( @Input() config : Config; )。

<div *ngFor="let thing of stuff; let config={index:index,junk:junk};">
  <app-thingy [data]="thing"
              [config]="config">
  </app-thingy>
</div>

When I access this.config (checking that it's been set in ngOnChanges() ) I see the values I'm setting but I can't access the method. 当我访问this.config (检查它是否已在ngOnChanges()设置)时,我看到我正在设置的值, 我无法访问该方法。 I also noticed that any additional parameters I might have in the constructor or possible public fields in the class aren't seen neither. 我还注意到,构造函数中可能包含的任何其他参数或类中可能的公共字段都不会被看到。 Basically, only the fields assigned explicitly in the HTML markup are available. 基本上,只有HTML标记中明确指定的字段可用。

ngOnChanges(changes: SimpleChanges): void {
  if (changes.config) {
    //console.log((new Config()).count());
    console.log((this.config.count()));
  }
}

The commented out line will produce an error saying that there's no such a method on the object. 注释掉的行将产生一个错误,表示对象上没有这样的方法。 The second line proves it wrong - there is such a method on the object. 第二行证明它是错误的 - 对象上有这样的方法。 I also tried casting it using this.config as Config but that led to the same result. 我也尝试使用this.config as Config但这导致了相同的结果。

I'm sensing that the object I'm passing from the HTML markup isn't at all typed Config and hence, it doesn't have any methods/variables I haven't assigned explicitly. 我感觉到我从HTML标记传递的对象并不是所有类型的Config ,因此,它没有任何我没有明确指定的方法/变量。 Since I can't just go new Config(...) in the markup (as asked for and responded to), I'm not sure how I'm supped to make the stupid computer trust me that it's dealing with a nicely implemented class including its convenience method. 因为我不能只在标记中添加new Config(...) (按照要求和回复),我不知道如何让愚蠢的计算机相信我它正在处理一个很好的实现课程包括其便利方法。

How should I deal with it? 我该怎么处理呢?

The best approach to it I can imagine is to construct an internal Config object making a copy of the fields from the passed in object and then to bind the HTML interpolators to that instead. 我能想象的最好的方法是构造一个内部Config对象,从传入的对象中复制字段,然后将HTML插值器绑定到该对象。 However, it seems soooo hacky that I'm certain it's either stupid or wrong (possibly both)... 然而,我觉得它确实是愚蠢或错误(可能两者)......

I don't know what you are trying to achieve with let config={index:index,junk:junk}; 我不知道你试图通过let config={index:index,junk:junk}; in your code, and that would throw an error, at least when I just now tried it. 在你的代码中,这会引发错误,至少在我刚刚尝试它时。 When removing this line and properly declaring a new Config in your parent, your code should work. 删除此行并在父级中正确声明新的Config时,您的代码应该可以正常工作。

Parent: 家长:

config = new Config(1,['one','two'])

Parent html: 父html:

<div *ngFor="let thing of stuff">
  <app-thingy [data]="thing" [config]="config"></app-thingy>
</div>

Child: 儿童:

ngOnChanges(changes: SimpleChanges): void {
  if (changes.config) {
    //console.log((new Config()).count());
    console.log((this.config.count()));
  }
}

This seems to work just fine! 这似乎工作得很好! DEMO: https://stackblitz.com/edit/angular-fnmk41?file=app%2Fapp.component.ts 演示: https//stackblitz.com/edit/angular-fnmk41? file = app% 2Fapp.component.ts

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

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