【问题标题】:How to refresh a blazor sub/child component within a main/parent component?如何刷新主/父组件中的 blazor 子/子组件?
【发布时间】:2020-04-01 06:12:27
【问题描述】:

你有一个主组件,在主组件内部你有很多子组件

你想刷新单个子组件,而不是主组件的整个屏幕,这可能吗?

【问题讨论】:

  • 您可以调用子组件的StateHasChanged方法让子组件刷新
  • @aguafrommars 在.razor 页面的@code 部分,如何引用特定的子组件?是否有与 ASP.NET 的 Page.FindControl() 对应的 Blazor?
  • @Tim 您可以使用@ref 获取所需组件的引用:docs.microsoft.com/en-us/aspnet/core/blazor/…

标签: c# blazor asp.net-core-3.0 blazor-server-side


【解决方案1】:

是的,有可能:

Parent.razor

<Child @ref="_childComponent" />
<button @onclick="@(() => _childComponent.Refresh())">Refresh Child</button>

@code {
    private Child _childComponent;
}

Child.razor

<p>Right now: @DateTime.Now</p>

@code {
    public void Refresh()
    {
        StateHasChanged();
    }
}

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 2021-07-04
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2022-12-22
    • 1970-01-01
    相关资源
    最近更新 更多