我如何从子组件在blazor获得属性



我需要从股票组件中获取计数我的父组件使用child.对于这段代码,Count得到0,但实际上数据有更多

<Heading Size="HeadingSize.Is4">Count @ItemCount Entries</Heading>
<StockComponent @ref=stockComponent />
StockComponent stockComponent;
int ItemCount ;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(firstRender)
{
ItemCount = stockComponent.Count();
StateHasChanged();
}
}

** My StockComponent **

private int count;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(firstRender)
{
Items = await db.Item.ToListAsync();
count =  Items.Count;
StateHasChanged();
}
}
public int Count () 
{
return count;
}

需要双向绑定。还:

  • 获取OnInitializedAsync()
  • 中的项目
  • 你不需要调用StateHasChanged()

最新更新