响应来自父组件的子事件

时间:2019-01-23 12:37:49

标签: c# eventemitter blazor

我有两个成分ABBA的子代。是否有EventEmitter({{1} }}中的Angular中?我如何在可以响应其子级输出的父级中附加事件处理程序?

孩子

Blazor

父组件

<button onclick="Emit">Press me</button>
@functions(){
[Parameter] // i do not know how the `Output` is called
private string Pressed {get;set;}
public void Emit()
{
  //somehow emit `Pressed` to the parent , make him respond
}
}

PS 对不起,语法错误,我还是很陌生。

2 个答案:

答案 0 :(得分:5)

您可以使用Action参数

getInstance

在Emit中,您可以使用条件语句检查是否有人订阅了Action并通过传入参数来调用它。

请不要忘记,在父级中,如果要更新页面,请在“ doSomething”方法中调用StateHasChanged()。

答案 1 :(得分:2)

组件A.cshtml

// Define a method in the parent component which will be called 
// from the child component when the user tap the button residing 
// in the child component. This method has a string parameter passed
// from the child component
public void GetValueFromChild(string value)
 {
        // Do somethig with value
  } 

B.cshtml组件


// When the user click the button the method GetValueFromChild
// defined on the parent component is called

<button class="btn" onclick=@(() => OnAddValue("some string value"))>Add</button>

    @functions
{
    // Define an Action delegate property which stores a reference
    // to A.GetValueFromChild
    // Parameters
    [Parameter] Action<string> OnAddValue{ get; set; }
}

A.cshtml

// Here (in the parent component) you place the child component and
// set its OnAddValue to the name of the method to be called
<B OnAddValue = "GetValueFromChild"></B> 

如果我的回答对您有帮助,请标记为已接受
希望这会有所帮助...