要在Blazor WebAssembly中实现在初始页面加载时执行某个方法,但在按钮点击时不执行该方法,可以使用以下步骤:
MyComponent.razor
的组件文件。@page "/mycomponent"
@using System
My Component
@code {
protected override void OnInitialized()
{
// 在初始化时执行的方法
Console.WriteLine("OnInitialized() called");
}
private void ButtonClicked()
{
// 在按钮点击时执行的方法
Console.WriteLine("ButtonClicked() called");
}
}
App.razor
文件,并将MyComponent
组件添加到路由配置中。
Sorry, there's nothing at this address.
My Component
运行Blazor WebAssembly项目。在浏览器中,点击“My Component”链接,将导航到MyComponent
组件的页面。
在浏览器的开发者工具中,打开控制台。您将看到在初始页面加载时,OnInitialized()
方法被调用,并输出消息"OnInitialized() called"。当您点击按钮时,ButtonClicked()
方法被调用,并输出消息"ButtonClicked() called"。
通过这种方式,您可以在初始化时执行某个方法,但在按钮点击时不执行该方法。请根据您的需求修改代码以满足您的实际情况。