[英]ReactJS: passing function to child component results in TypeError
[英]Angular2 passing function results to child component - “this.bar is not a function”
我正在嘗試將功能數據傳遞給孩子,但是SO上的所有答案都來自(alfa / beta)中的Angular2,而且我不知道我的問題是否源於自官方發布以來情況已經發生變化的事實。 我已經嘗試過所有關於SO的答案,但是它似乎並沒有消除我的錯誤,即“ this.bar不是函數”
現在,我只是試圖通過一個函數傳遞一個簡單的字符串:
父組件:
export class ParentComponent implements OnInit {
foo() {
return "This is a parent func stream value";
}
}
父HTML
<child [bar]="foo()"></child>
子組件
export class ChildComponent implements OnInit {
@Input() bar : Function;
ngOnInit() {
this.bar();
}
我看不到問題,請幫我看看!
編輯
上面代碼中的“函數”類型在我的編輯器中仍然是白色,這困擾着我,但是,就像下面的示例一樣,當我將代碼粘貼到組件下面時,相同的“函數”就變成了藍色,就像其他類型一樣。我的代碼,所以我試圖弄清楚為什么會這樣...
子組件
export class ChildComponent implements OnInit {
@Input() bar : Function; //The word "Function" here is white when it should be blue
ngOnInit() {
this.bar();
}
}
@Input() bar: Function // This word "Function" is blue like it should be
functionResult: any;
ngOnInit() {
this.functionResult = this.bar();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.