Install-Package ComponentBuilder
using ComponentBuilder;
//...
builder.Services.AddComponentBuilder();
//...
@inherits BlazorComponentBase
<span @attributes="AdditionalAttributes"> <!--@attributes 是必须的-->
@ChildContent
</span>
@code{
[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter][CssClass("active")] public bool Active { get; set; }
}
<Element>Hello</Element>
<span>Hello</span>
<Element Active>Active Hello</Element>
<span class="active">Active Hello</span>
[HtmlTag("span")]
public class Element : BlazorComponentBase, IHasChildContent
{
[Parameter] public RenderFragment? ChildContent { get; set; }
[Parameter][CssClass("active")] public bool Active { get; set; }
}
<Element>Hello</Element>
<span>Hello</span>
<Element Active>Active Hello</Element>
<span class="active">Active Hello</span>
如您所见,使用组件类可以完全自动化组件,但缺点是它需要一定的具体思维,尤其是在处理复杂组件时。当然,您可以将两者混合使用,但只要能提高效率,就是个好主意。