blazor - 无法制作嵌套组件
问题描述
抱歉,如果问题与我缺乏经验有关。我正在尝试制作一个组件库来处理来自 blazor 的地图框。现在我有一个主要组件 MapBoxView,它显示地图和 MapboxGeocoder,用于查找对象。每个组件在主组件 OnFirstAfterRenderAsync() 中都有一个过程:
protected async override Task OnFirstAfterRenderAsync()
{
await base.OnFirstAfterRenderAsync();
Dictionary<string, object> o = new Dictionary<string, object>();
...
await JsInvokeAsync<object>("mapgl.mymap.initMap", o);
}
在嵌套组件中:
protected async override Task OnFirstAfterRenderAsync()
{
await base.OnFirstAfterRenderAsync();
Dictionary<string, object> o = new Dictionary<string, object>();
o.Add("container", container);
await JsInvokeAsync<object>("mapgl.mygeocoder.initGeocoder", o);
}
在演示程序中我使用代码:
@page "/"
<MapBoxView zoom=@_zoom container=@containerName mapCenter=@centerOfView>
</MapBoxView>
<MapBoxGeocoder Id="geocoder">
</MapBoxGeocoder>
在演示程序中我使用代码:
<MapBoxView zoom=@_zoom container=@containerName mapCenter=@centerOfView>
<MapBoxGeocoder Id="geocoder">
</MapBoxGeocoder>
</MapBoxView>
也就是说,我把嵌套组件放在主组件中,然后地理编码器(左下角有文本的窗口 - 图片上的搜索)没有显示,程序没有进入 MapBoxGeocoder 组件的 OnFirstAfterRenderAsync() 过程。主要组件 MapBoxView 有剃须刀代码:
@namespace MapBoxBlazor
@inherits BaseMapBoxView
<div id='mapContainer'>
</div>
我究竟做错了什么?怎么做才对?
解决方案
您应该查看有关ChildContent的文档。
您需要使用RenderFragment
命名ChildContent
来允许您渲染嵌套组件。
父组件必须有属性
public RenderFragment ChildContent { get; set; }
并将其呈现在您想要的位置。例如
<div>
@ChildContent
</div>
此属性将渲染父组件内的所有内容。
所以对于你的情况,MapBoxView
应该有ChildContent
它并在里面渲染它才能工作。
推荐阅读
- css - CSS ID WooCommerce 商店页面
- excel - 如何比较 Excel VBA 单元格中的上一个、当前和下一个元素?
- flutter - 颤动如何通过点击导航栏加载新屏幕
- fluentd - parse_audit_log 不适用于 td-agent
- php - Shopware - 在 orderRepository 中获取 documentCollection
- c++ - 为什么重载的“+=”和“<<”不能一起工作?
- r - 如何显示当前观察的百分比?
- ubuntu - 运行命令 sudo visudo ,现在我遇到了一些错误
- wso2 - 什么是 WSO2 oauth2 授权端点
- php - 为 Backpack 管理面板添加 SSO 登录