【问题标题】:(Blazor) Why child razor component not working?(Blazor)为什么儿童剃须刀组件不起作用?
【发布时间】:2021-02-16 19:46:21
【问题描述】:

我正在尝试将子组件包含到父组件中

但是子组件不工作

下面是代码

==================== parent component razor ====================
<InfoDialog @ref="infoDialog"></InfoDialog>
==================== parent component code behind ====================
public InfoDialog infoDialog { get; set; }

public async Task AddToDB()
{
    infoDialog.Show("success", "request complete!");
}
==================== child component razor ====================
<MatDialog @bind-IsOpen="@IsShow">
    <MatDialogTitle>@Title</MatDialogTitle>
    <MatDialogContent>
        <p>@Description</p>
    </MatDialogContent>
    <MatDialogActions>
        <MatButton OnClick="@Close">OK</MatButton>
    </MatDialogActions>
</MatDialog>
==================== child component code behind ====================
public partial class InfoDialog
{
    public bool IsShow { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }

    public void Show(string _title, string _Description)
    {
        IsShow = true;
        Title = _title;
        Description = _Description;
    }
    public void Close()
    {
        IsShow = false;
    }
}

当我调用'AddToDB'函数时,我调用了子组件的show函数,但是子组件没有被渲染。谁能帮帮我?

对不起我的英语不好

【问题讨论】:

  • 我添加了 this.StateHasChanged();在 infoDialog.Show() 下。但现在也醒了

标签: asp.net-core .net-core parent-child blazor code-behind


【解决方案1】:

尝试在您的子组件方法中调用StateHasChanged()

public void Show(string _title, string _Description)
{
    IsShow = true;
    Title = _title;
    Description = _Description;
    StateHasChanged();
}

public void Close()
{
    IsShow = false;
    StateHasChanged();
}

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2020-08-11
    • 2020-05-10
    • 2020-01-28
    • 2020-02-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    相关资源
    最近更新 更多