有没有办法在 html 标签内的 blazor 组件中执行代码?

Is there a way to execute code in blazor component inside html tags?

我想做的是这样的:

<input type="text"
@if(!String.IsNullOrEmpty(Element.Placeholder))
{
   placeholder="@Element.Placeholder"
}
/>

我想用许多属性来做到这一点,比如最大长度、值等。 我想出的最佳解决方案是使用 MarkUpString 执行上述操作然后渲染它。我想知道是否有一种方法可以在 html 标记内使用 if's,这样我的代码将更清晰且更易于修改。请宽容,我是编程新手,尤其是 Blazor。谢谢

*编辑:我希望这个组件也可以从另一个组件进行编辑。

您可以使用 @attributes。我在下面展示了一个非常简单的代码示例来设置样式。

<button @attributes="this.ButtonAttributes">Red Button</button>
@code{

  private Dictionary<string, object> ButtonAttributes = new Dictionary<string, object>();

  protected override Task OnInitializedAsync()
  {
    ButtonAttributes.Add("class", "btn btn-danger");
    return Task.CompletedTask;
  }

}