【发布时间】:2021-12-27 15:07:41
【问题描述】:
我正在尝试使用 @if 块有条件地将渲染片段传递给组件,但出现构建错误
Unrecognized child content inside component 'XXX'.
如果我有这样的父组件 AwesomeComponent:
@code
{
[Parameter] public RenderFragment ItemTemplate {get; set;}
[Parameter] public RenderFragment AnotherItemTemplate {get; set;}
[Parameter] public RenderFragment YetAnotherItemTemplate {get; set;}
}
然后有条件地想像这样渲染一个渲染片段:
<AwesomeComponent>
<ItemTemplate>
// some code here
</ItemTemplate>
<AnotherItemTemplate>
// blabla
</AnotherItemTemplate>
// This one i want to pass conditionally, the intuitive way is like this
@if (condition)
{
<YetAnotherItemTemplate>
// wooho
</YetAnotherItemTemplate>
}
我收到Unrecognized child content inside component 错误。
我见过像this 这样的解决方案。但是,我也遇到了第三方组件的这些问题,因此我无法控制那里建议的可见性。此外,我不认为这是一个优雅的解决方案。
有没有人在类似的情况下找到了一种方法来处理所描述的条件渲染片段?我发现它在 Blazor 中是一个很大的限制。
【问题讨论】:
-
“我发现它在 Blazor 中是一个很大的限制。”问题不在于 Blazor