为什么等待 InvokeAsync(StateHasChanged());或 StateHasChanged() 不更改任何内容或在 Blazor 中重新呈现?
Why does await InvokeAsync(StateHasChanged()); or StateHasChanged() not change anything or re-render in Blazor?
我想在删除或创建新产品时刷新 Table 组件。
我尝试调用 await InvokeAsync(StateHasChanged());
或 StateHasChanged()
但是 Table 组件没有刷新它的数据,我必须手动点击刷新按钮才能看到变化
public async Task HandleValidSubmit()
{
ReceivedProduct = new MyBlazorApp.Models.Product();
using (var httpClient = new HttpClient())
{
StringContent content = new StringContent(JsonConvert.SerializeObject(ProductData), Encoding.UTF8, "application/json");
using (var response = await httpClient.PostAsync("https://localhost:7104/api/Products", content))
{
string apiResponse = await response.Content.ReadAsStringAsync();
ReceivedProduct = JsonConvert.DeserializeObject<MyBlazorApp.Models.Product>(apiResponse);
}
}
FormSubmitMessage = "Product Created";
await InvokeAsync(StateHasChanged);
}
protected async Task Delete(int Id)
{
await client.DeleteAsync("api/products/" + Id);
FormSubmitMessage = "Product with ID: " + Id + " is deleted.";
await InvokeAsync(StateHasChanged);
}
在这两种情况下调用 StateHasChanged() 都是不必要的 - 它会在事件处理程序之后被调用。
But the Table component doesn't refresh its data,
您需要重新加载数据。大致:
protected override async Task OnInitializedAsync()
{
await LoadTable();
}
....
protected async Task Delete(int Id)
{
await client.DeleteAsync("api/products/" + Id);
FormSubmitMessage = "Product with ID: " + Id + " is deleted.";
//await InvokeAsync(StateHasChanged);
await LoadTable();
}
我想在删除或创建新产品时刷新 Table 组件。
我尝试调用 await InvokeAsync(StateHasChanged());
或 StateHasChanged()
但是 Table 组件没有刷新它的数据,我必须手动点击刷新按钮才能看到变化
public async Task HandleValidSubmit()
{
ReceivedProduct = new MyBlazorApp.Models.Product();
using (var httpClient = new HttpClient())
{
StringContent content = new StringContent(JsonConvert.SerializeObject(ProductData), Encoding.UTF8, "application/json");
using (var response = await httpClient.PostAsync("https://localhost:7104/api/Products", content))
{
string apiResponse = await response.Content.ReadAsStringAsync();
ReceivedProduct = JsonConvert.DeserializeObject<MyBlazorApp.Models.Product>(apiResponse);
}
}
FormSubmitMessage = "Product Created";
await InvokeAsync(StateHasChanged);
}
protected async Task Delete(int Id)
{
await client.DeleteAsync("api/products/" + Id);
FormSubmitMessage = "Product with ID: " + Id + " is deleted.";
await InvokeAsync(StateHasChanged);
}
在这两种情况下调用 StateHasChanged() 都是不必要的 - 它会在事件处理程序之后被调用。
But the Table component doesn't refresh its data,
您需要重新加载数据。大致:
protected override async Task OnInitializedAsync()
{
await LoadTable();
}
....
protected async Task Delete(int Id)
{
await client.DeleteAsync("api/products/" + Id);
FormSubmitMessage = "Product with ID: " + Id + " is deleted.";
//await InvokeAsync(StateHasChanged);
await LoadTable();
}