我正在创建一个自InputBase派生的自定义组件...
到目前为止,这是我的MyCustomInput.razor代码:
@inherits InputBase<string>
<input id="@id" @attributes="AdditionalAttributes" class="@CssClass" value="@CurrentValue"
我希望能够使用该组件之一:
<MyCustomInput @bind-Value="user.Telephone" ...
或
<MyCustomInput @bind-Value="user.Telephone" @bind-Value:event="oninput" ...
要完成此操作,我应该更改为MyCustomInput吗?
非常感谢您的帮助!
干杯
拉斐尔
答案 0 :(得分:1)
MyCustomInput.razor
@inherits Microsoft.AspNetCore.Components.Forms.InputText
<input @attributes="@AdditionalAttributes" @bind="@CurrentValueAsString" @bind:event="oninput" />
用法:
<MyCustomInput @bind-Value="user.Telephone" class="@CssClass" id="@id" />
无需使用字符串覆盖TryParseValueFromString
。
html属性id
和class
将通过喷溅@attributes="@AdditionalAttributes
答案 1 :(得分:0)
您可以将组件嵌套在其他组件中。有关如何执行此操作的示例,请参见this answer。