【问题标题】:How can i get properties from child component in blazor如何从 blazor 中的子组件获取属性
【发布时间】:2021-12-11 16:12:16
【问题描述】:

我需要从 stockcomponents 获取计数 我的父组件使用子组件。对于此代码,计数为 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();
    }
}

** 我的 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;
}

【问题讨论】:

    标签: asp.net-mvc asp.net-core blazor


    【解决方案1】:

    您需要two-way binding。 另外:

    • OnInitializedAsync()中获取项目
    • 您不必致电StateHasChanged()

    【讨论】:

      猜你喜欢
      • 2021-05-27
      • 2022-10-13
      • 2017-05-11
      • 2019-12-15
      • 2021-06-14
      • 1970-01-01
      • 2015-07-25
      • 2018-05-06
      • 1970-01-01
      相关资源
      最近更新 更多