【问题标题】:Blazor run custom event when child component changes parent component values当子组件更改父组件值时,Blazor 运行自定义事件
【发布时间】:2020-03-07 21:55:10
【问题描述】:

我有一个父子组件:

父组件

<IncrementButton Increment="-1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" />
<span>@Amount</span>
<IncrementButton Increment="1" @bind-Attribute="Amount" @onclick="@(() => UpdateBottomPillText())" />
<span>@bottomPillAmount</span>

@code {
    [Parameter] public int Amount { get; set; }
    [Parameter] public EventCallback<int> AmountChanged { get; set; }

    string bottomPillAmount;

    string UpdateBottomPillText()
    {
        double amountAdded = Math.Floor(((double)(Amount - 10.0)) / 2.0);

        bottomPillAmount = amountAdded > 0 ? "+" + amountAdded.ToString() : amountAdded.ToString();

        return Amount.ToString();
    }

    protected override void OnParametersSet()
    {
        UpdateBottomPillText();
    }

}

子组件 (IncrementButton)

<button type="button" @onclick="OnAttributeChanged">@displayText</button>

@code {
    [Parameter]
    public int Increment { get; set; } = 1;

    [Parameter]
    public int Attribute { get; set; }

    [Parameter]
    public EventCallback<int> AttributeChanged { get; set; }

    [Parameter]
    public EventCallback<MouseEventArgs> OnClick { get; set; }

    string displayText;

    protected override void OnParametersSet()
    {
        displayText = Increment > 0 ? "+" + Increment.ToString() : Increment.ToString();
    }

    private Task OnAttributeChanged()
    {
        Attribute += Increment;
        await OnClick.InvokeAsync(new MouseEventArgs());
        return AttributeChanged.InvokeAsync(Attribute);
    }
}

@bind-Attribute="Amount" 工作并在按下IncrementButton 时更新值,但我想运行UpdateBottomPillText() 并更新值。现在我通过onclick 事件来做这件事,但是有没有办法在父组件中修改AttributeChanged 事件?

【问题讨论】:

  • 这是非编译代码。修复它并复制/粘贴正在工作或显示问题的内容。

标签: blazor blazor-server-side


【解决方案1】:

这里需要适当设置组件之间的双向数据绑定。

IncrementButton.razor

<button type="button" @onclick="OnAttributeChanged">@displayText</button>

@code {
[Parameter]
public int Increment { get; set; } = 1;

[Parameter]
public int Attribute { get; set; }

[Parameter]
public EventCallback<int> AttributeChanged { get; set; }


string displayText;

protected override void OnParametersSet()
{
    displayText = Increment > 0 ? "+" + Increment.ToString() : 
                                                Increment.ToString();
}

private Task OnAttributeChanged()
{
    Attribute += Increment;
    return AttributeChanged.InvokeAsync(Attribute);
}
}

用法

<IncrementButton Increment="-1" @bind-Attribute="Amount"/>
<span>@Amount</span>
<IncrementButton Increment="1" @bind-Attribute="Amount"/>
<span>@bottomPillAmount</span>

@code {
  private int amount;
  [Parameter] public int Amount
  {
    get => amount;
    set {
        if(amount != value)
        {
            amount = value;
            UpdateBottomPillText();

        }
    }
 }

 string bottomPillAmount;

string UpdateBottomPillText()
{
    double amountAdded = Math.Floor(((double)(Amount - 10.0)) / 2.0);

    bottomPillAmount = amountAdded > 0 ? "+" + amountAdded.ToString() : amountAdded.ToString();

    return Amount.ToString();
}

protected override void OnParametersSet()
{
    UpdateBottomPillText();

}

}

【讨论】:

  • 太棒了,效果很好。我的第二个问题是如何对自定义对象中的属性做同样的事情?我发布了我的问题here,如果你可以看看:)
猜你喜欢
  • 1970-01-01
  • 2022-01-13
  • 2020-11-16
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
  • 2019-07-28
  • 2018-04-22
相关资源
最近更新 更多