我在这本指南。之后向我现有的MVC应用程序中添加了Blazor组件,这是按预期工作的,而且我能够让Blazor组件呈现,但是我在创建一个简单的列表时遇到了问题,因为我无法正确地获得组件嵌套。
我已经得到以下代码工作在一个Blazor应用程序正确地呈现一个简单的列表;
SimpleList.blazor
<ol>
@ChildContent
</ol>
@code {
[Parameter]
public RenderFragment ChildContent { get; set; }
}SimpleListItem.blazor
<li>
<span>@Text</span>
</li>
@code {
[Parameter]
public string Text { get; set; }
}Index.razor
<SimpleList>
<SimpleListItem Text="Item 1"></SimpleListItem>
<SimpleListItem Text="Item 2"></SimpleListItem>
</SimpleList>当在上面显示的blazor应用程序中使用这些组件时,这会产生包含两个项的有序列表的预期结果。但是,由于在MVC应用程序不支持中直接使用了blazor组件名,因此我正在使用帮助器标记,目前正在执行以下操作:
Index.cshtml
<component type="typeof(Components.SimpleList)" render-mode="Server">
<component type="typeof(Components.SimpleListItem)" render-mode="Server" param-Text="@("Item 1")"/>
<component type="typeof(Components.SimpleListItem)" render-mode="Server" param-Text="@("Item 2")"/>
</component>但是,这是不工作的,列表项没有呈现在列表中,并且正在生成以下HTML
生成的HTML
<ol><!--!--></ol>
<li><span>Item 1</span></li>
<li><span>Item 2</span></li>我是做错了什么,还是在MVC应用程序中不支持嵌套组件?
发布于 2021-09-13 11:30:58
<component>标记本身不能以这种方式嵌套。它可以呈现一个单个组件(它本身可能有嵌套的Razor组件)。
如果Index.razor在此上下文中是可访问的,则可以尝试
<component type="typeof(Components.Index)" render-mode="Server">
</component>标记助手正在呈现第一个组件SimpleList,但是子内容是空的,因此
<ol><!--!--></ol>然后标记助手呈现内部项:两个SimpleListItem引用,结果是
<li><span>Item 1</span></li>
<li><span>Item 2</span></li>https://stackoverflow.com/questions/69151877
复制相似问题