我嵌套了 Blazor 组件:MainLayout(从示例项目修改)> Expeditions > EditExpedition > UpdateExpVessels,如下所示。
问题是我在 UpdateExpVessels 中使用 onclick 来调用本地方法,但由于嵌套(我假设),它没有找到。错误消息为: Uncaught ReferenceError ReferenceError: FlipEdit is not Defined 在 onclick (localhost꞉7281/editExpedition/1000:1:1)
我需要做一些特殊的事情才能在嵌套结构中的同一 Blazor 页面上调用方法吗?
在主布局中:
<div class="top-row px-4 small-font">
<Expeditions />
在探险中:
string path = "/editExpedition/" + expedition.ExpeditionID;
<DropdownItem To="@path" Type="ButtonType.Link">@expedition.Name</DropdownItem>
在编辑远征中:
<td width="25%"><UpdateExpVessels ID="@ID" /></td>
在UpdateExpVessels中:
<Icon Name=IconName.Pencil Color=IconColor.Success onclick="FlipEdit"/>
@code {
[Parameter]
public int? ID { get; set; }
private bool editPage = true;
public void FlipEdit()
{
editPage = !editPage;
}
}
Blazor 处理点击和其他事件的方式略有不同 您正在尝试以传统的 HTML 方式绑定事件
<Icon Name=IconName.Pencil Color=IconColor.Success onclick="FlipEdit"/>
Blazor 不知道如何在组件类上查找并执行 hte 方法,而是使用
<Icon Name=IconName.Pencil Color=IconColor.Success @onclick="FlipEdit"/>
这应该找到方法,我不确定 Icon 是否接受回调,所以我将它包裹在一个按钮中:
<button @onclick="FlipEdit">
<Icon Name=IconName.Pencil Color=IconColor.Success />
</button>