Blazor子组件EventCallBack没有响应

时间:2020-10-20 07:16:27

标签: c# components blazor blazor-server-side blazor-client-side

此处页面组件

@page "/student"
<h3>Student List</h3>
<h5>@StudentsList</h5>

<StudentDetail StudentName="Something New" ButtonClicked="@Save"></StudentDetail>

<h5>This is second</h5>
<h5>@SaveMessage</h5>


@code {
    public string StudentsList { get; set; }
    private string SaveMessage { get; set; }

    protected override async Task OnInitializedAsync()
    {
        StudentsList = "ASD DSA KDS CFA";
    }

    private void Save(MouseEventArgs e)
    {
        SaveMessage = "Operation Saved.";
    }
}

这是StudentDetail

<h3>Student Detail for @StudentName</h3>
<button class="btn btn-primary" @onClick="ButtonClicked">Click Me!</button>
@code {
    [Parameter]
    public string StudentName { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> ButtonClicked { get; set; }

}

我在服务器端和客户端都尝试过此操作。当我单击“单击我!”按钮,绝对没有任何反应。我在做什么错了?

这些是进口

@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop
@using BlazorApp4
@using BlazorApp4.Shared

1 个答案:

答案 0 :(得分:1)

StudentDetail:

它是@onclick中的@onClick,而不是@onClick="ButtonClicked"

当您单击"Click Me!"按钮时,您实际上想在Student组件中触发“保存”方法。这是您的操作方式:

`@onclick="() => ButtonClicked.InvokeAsync()"`

学生: 将ButtonClicked="@Save"更改为ButtonClicked="Save"