我试图根据某些条件将我的组件之一包装在某个特定标签上。
为了简单起见,假设我有一个
<div class="inner-class">
this must be in inner-class div and wrapped in some-class div
</div>
如果 'condition' == true 那么它应该将它包装在另一个 div 中,结果应该是这样的
<div class="wrapper-class">
<div class="inner-class">
this must be in inner-class div and wrapped in some-class div
</div>
</div>
在你说之前,使用 if-else 方法。我必须说不。因为标签和其中的类是动态的,所以我不能那样写。
我想做的是
@if (condition)
{
@:<div class="wrapper-class">
}
<div class="inner-class">
this must be in inner-class div and wrapped in some-class div
</div>
}
@if (condition)
{
@:</div>
}
我认为它应该完成这项工作。 但问题是浏览器在将内部 div 放入其中之前关闭了外部 div。
最佳答案
你必须使用BuildRenderTree
使用 BuilderRenderTree,您可以完全控制构建组件 html。
有关更多信息,请阅读这篇好文章 Building Components Manually via RenderTreeBuilder
关于c# - Blazor 关闭有条件的 div 包装器上的 div 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67367345/