Blazor 按钮在子页面中工作,但不适用于主页(新的 Blazor WebApp 项目)

问题描述 投票:0回答:1

我无法在主页或主页上的组件上使用按钮。 具有相同代码的辅助页面可以工作。

这是我的主页,点击不起作用。 按下去没有任何影响。

@page "/"


<PageTitle>Home</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<MyComponent  />

@code
{
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

这是位于主页上的 MyComponent。 它有主页代码的复制/粘贴,点击没有任何反应

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

然后这里是项目自带的Counter页面。 单击按钮在这里起作用。

@page "/counter"
@rendermode InteractiveWebAssembly

<PageTitle>Counter</PageTitle>

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }
}

唯一的区别是 Counte.razor 页面上的 @rendermode InteractiveWebAssembly,但如果我将其放在主页上,那么应用程序就会崩溃。

这是使用新的 Blazor WebApp 项目创建的基本项目。 我使用的是.NET 8 的最新 VS (17.10.3)。

c# .net blazor-webassembly
1个回答
0
投票

在此项目模板中,主页是服务器端,而计数器页面是 WASM。 这就是主页没有响应的原因。

© www.soinside.com 2019 - 2024. All rights reserved.