簡體   English   中英

Angular2將函數結果傳遞給子組件-“ this.bar不是函數”

[英]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();
  }

foo()實際上不是一個函數。 它求值為函數調用的返回值。 您的輸入bar需要一個函數作為其類型。

您應該嘗試像<child [bar]="foo"></child>

編輯
查看工作的矮人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM