[英]Blazor Wasm - CascadingValue update from descending component
I want a global loading indicator on my application.我想在我的应用程序上使用全局加载指示器。 I have already a component for that, LoadingIndicator.razor:
我已经有一个组件,LoadingIndicator.razor:
<CascadingValue Value="this">
<TelerikLoaderContainer Visible="@( IsVisible )" Text="@Text" />
@ChildContent
</CascadingValue>
@code {
[Parameter]
public RenderFragment? ChildContent { get; set; }
[Parameter]
public bool IsVisible { get; set; }
[Parameter]
public string Text { get; set; } }
and somewhere in my component tree I try the following, childComponent.razor在我的组件树中的某处,我尝试以下操作,childComponent.razor
[CascadingParameter]
protected LoadingContainer LoadingContainer { get; set; }
protected override async Task OnInitializedAsync()
{
LoadingContainer.Text = "test";
LoadingContainer.IsVisible = true;
}
according to the warning BL0005
thats not allowed due to inconsistent Ui (possibly).根据警告
BL0005
,由于 Ui 不一致(可能),不允许这样做。 But how could I show the loading indicator on my child components?但是我怎样才能在我的子组件上显示加载指示器呢? I would not like to use inheritance because I want to be flexible whether and where I want to use it.
我不想使用 inheritance,因为我想灵活地使用它,无论是在何处使用它。 The CascadingValue is applied at the
MainLayout
level so all components have it available. CascadingValue 应用于
MainLayout
级别,因此所有组件都可用。
You want to trigger an update in the parent.您想要在父级中触发更新。
<CascadingValue Value="this">
<TelerikLoaderContainer Visible="@( IsVisible )" Text="@Text" />
@ChildContent
</CascadingValue>
@code {
[Parameter]
public RenderFragment? ChildContent { get; set; }
//[Parameter] -- you can make these private fields now
public bool IsVisible { get; set; }
//[Parameter] -- same
public string Text { get; set; }
public void ShowLoader(string text)
{
Text = text;
IsVisible = true;
StateHasChanged();
}
}
and use it like this并像这样使用它
protected override async Task OnInitializedAsync()
{
//LoadingContainer.Text = "test";
//LoadingContainer.IsVisible = true;
LoadingContainer.ShowLoader("test");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.