我在页面(组件)上有一个按钮,点击后会调用Refresh()方法。然后该方法会调用StateHasChanged(),但不会重新加载页面。 GetData() 调用外部 API 从数据库中加载数据。
<button class="btn btn-warning" @onclick="Refresh">Refresh</button>
code {
protected override async Task OnInitializedAsync()
{
try
{
await GetData();
base.OnInitialized();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
protected async Task GetData()
{
try
{
results = await HttpClient.GetJsonAsync<Results[]>(ServiceEndpoints.GET_RESULTS);
}
catch (Exception ex)
{
Console.WriteLine("Error retrieving data from Oracle.", ex.Message);
}
}
public void Refresh()
{
StateHasChanged();
}
我的页面(组件)还有一个表单,其中包含可以更改的预加载输入。我希望用户能够编辑表单,但点击按钮以获取原始数据来刷新页面。这在之前的 Blazor 版本上是可行的,是否已知此问题?
@page "/"
@inherits IndexBase
<EditForm Model="@results">
<label><strong>Select a job to view its current parameters</strong></label>
<div class="currentJobForm">
<InputSelect id="jobSelect" @bind-Value="jobSelected">
@foreach (var items in results)
{
<option value="@items.JOB_NAME">@items.JOB_NAME</option>
}
</InputSelect>
<button class="btn btn-warning" @onclick="SaveChanges" disabled="@IsDisabled">Save</button>
<button class="btn btn-warning" @onclick="Refresh">Refresh</button>
<button class="btn btn-danger" @onclick="DeleteJob">Delete Job</button>
</div>
<label><strong>Notify Parameters</strong></label>
<div class="notifyParametersForm">
@foreach (var item in results.Where(i => i.JOB_NAME == jobSelected))
{
<div class="issueDescription">
<label><strong>Issue Description</strong></label>
<InputText id="issueDesc" @bind-Value="item.JOB_HEADER" placeholder="Enter New Issue Description" />
</div>
<div class="sendSlack">
<label><strong>Send Slack</strong></label>
<InputSelect id="sendSlackSelect" @bind-Value="item.SENDSLACK">
@foreach (var items in InitializeData.SendSlacks)
{
<option value="@items.SendSlackName">@items.SendSlackName</option>
}
</InputSelect>
</div>
<div class="slackUser">
<label><strong>Slack User</strong></label>
<InputText id="slackUser" @bind-Value="item.SLACK_USER" placeholder="Enter New Slack User" />
</div>
<div class="slackChannel">
<label><strong>Slack Channel</strong></label>
<InputSelect id="sendSlackChannel" @bind-Value="item.SLACK_CHANNEL">
@foreach (var items in InitializeData.SlackChannels)
{
<option value="@items.SlackChannelName">@items.SlackChannelName</option>
}
</InputSelect>
</div>
<div class="slackUrl">
<label><strong>Slack URL</strong></label>
<InputText id="slackUrlTextBox" @bind-Value="item.SLACK_URL" placeholder="Enter New Slack Url" />
</div>
<div class="sendMail">
<label><strong>Send Mail</strong></label>
<InputSelect id="sendMailSelect" @bind-Value="item.SENDMAIL">
@foreach (var items in InitializeData.SendMails)
{
<option value="@items.SendMailName">@items.SendMailName</option>
}
</InputSelect>
</div>
<div class="mailFrom">
<label><strong>From:</strong></label>
<InputText id="from" @bind-Value="item.MAILFROM" placeholder="Enter New Mail From" />
</div>
<div class="mailTo">
<label><strong>To:</strong></label>
<InputText id="to" @bind-Value="item.MAILTO" placeholder="Enter New Mail To" />
</div>
<div class="subject">
<label id="subjectLabel"><strong>Subject:</strong></label>
<InputText id="subject" @bind-Value="item.EMAIL_SUBJECT" placeholder="Enter New Subject" />
</div>
}
</div>
</div>
StateHasChanged
不会刷新页面,它只会根据Render Tree
更新任何需要更新的 dom 元素。如果没有看到所有代码,很难进一步评论,但某种程度上,Blazor 可能认为没有任何需要更新的元素。我猜测输入已经绑定回 Blazor,而 Blazor 将当前 dom 视为与渲染树相同。您需要先将输入设置回其原始值。 - Kyle