简体   繁体   English

如何使用同一方法多次刷新父组件中的子组件?

[英]How can I refresh a child component from a parent component multiple times from the same method?

Right now my child component only updates once the GetLeads() method in the parent component is finished.现在我的子组件只有在父组件中的GetLeads()方法完成后才会更新。 I never see the spinner or Searching text.我从来没有看到微调或搜索文本。

Parent Component:父组件:

<SearchResultComponent  @ref="ChildComponent"></SearchResultComponent>

Code:代码:

 protected SearchResultComponent ChildComponent;
 public int LeadsFound { get; set; }
 public void GetLeads()
 {
      ChildComponent.Refresh(true, 0);
      var leads  = _searchService.Search(searchRequest);
      LeadsFound = leads?.Count ?? 0;
      _writeFileService.WriteToFile(leads);
      ChildComponent.Refresh(false, LeadsFound);
  }

Child Component:子组件:

@code {

    public bool Searching { get; set; } = false;
    public int LeadsFound { get; set; } = 0;
    public void Refresh(bool searching, int leadsFound)
    {
        Searching = searching;
        LeadsFound = leadsFound;
        StateHasChanged();
    }
}

@if (Searching)
{
    <div>Searching...</div>
    <div>
        <img src="~/Content/searching-spinner.gif" />
    </div>
}
else
{
    <div>Leads Found: @LeadsFound</div>
}

my child component only updates once我的子组件只更新一次

You can make the eventhandler async.您可以使事件处理程序异步。 That allows you then to call Task.Delay(1) or Task.Yield() and that will effectuate the StateHasChanged().然后,您可以调用Task.Delay(1)Task.Yield() ,这将影响 StateHasChanged()。

The call form @onclick="@GetLeads" can remain the same.调用表单@onclick="@GetLeads"可以保持不变。 Note that you don't need the 2nd @ in there.请注意,您不需要那里的第二个@

If your searchService.Search() was async then you wouldn't need Task.Yield()如果您的searchService.Search()是异步的,那么您将不需要Task.Yield()

 public async Task GetLeads()
 {
      ChildComponent.Refresh(true, 0);      
      await Task.Yield();  // enable the render thread to run.
      var leads  = _searchService.Search(searchRequest);
      LeadsFound = leads?.Count ?? 0;
      _writeFileService.WriteToFile(leads);
      ChildComponent.Refresh(false, LeadsFound);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个 Blazor(“子”)组件调用一个 Blazor(父)组件中的方法? - How do I call a method in one Blazor (parent) component from another Blazor (“child”) component? 使用 StateHasChanged 从父组件刷新 blazor 服务器子组件 - Refresh blazor server child component from parent component by using StateHasChanged Blazor 组件:从子组件更新模型时刷新父组件 - Blazor component : refresh parent when model is updated from child component 如何将列表从子组件传递到 blazor 中的父组件? - How can I pass List from child component to parent component in blazor? 如何将数据从子组件传递给父组件 - How to pass data from child component to parent 如何从父组件中的事件更新子组件属性 - How to update child component properties from event in parent component 如何以角度将数据从父组件传递到自动完成子组件 - How to Pass data to autocomplete child component from parent component in angular 如何将可选的 RenderFragment 从父组件传递给 Blazor 中的子组件? - How to pass an optional RenderFragment from a parent component to a child component in Blazor? 如何将字符串和命令从父组件传递到子组件 - How to pass strings and commands from parent component to child component 如何在WPF中从子组件到父组件捕获已激活的命令? - How to catch activated command from child component to parent component in WPF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM