Blazor 子组件 EventCallBack 没有响应
Blazor Child Component EventCallBack not responding
这里是页面组件
@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
学生详情:
在@onClick="ButtonClicked"
中是@onclick
,而不是@onClick
当你点击"Click Me!"
按钮时,你实际上是想触发Student组件中的“Save”方法。这就是你的做法:
`@onclick="() => ButtonClicked.InvokeAsync()"`
学生:
将 ButtonClicked="@Save"
更改为 ButtonClicked="Save"
这里是页面组件
@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
学生详情:
在@onClick="ButtonClicked"
@onclick
,而不是@onClick
当你点击"Click Me!"
按钮时,你实际上是想触发Student组件中的“Save”方法。这就是你的做法:
`@onclick="() => ButtonClicked.InvokeAsync()"`
学生:
将 ButtonClicked="@Save"
更改为 ButtonClicked="Save"