【发布时间】:2020-10-08 10:16:11
【问题描述】:
是否可以使用父组件方法@onclick,还是我需要从子组件调用它?
假设我想调用父方法 Foo()。
家长
@page "/"
// Custom component button
<Button Text="Some text" @onclick="Foo" Apperance="@Style.secondary" />
@code {
async Task Foo()
{
// ...
}
}
儿童
<button class="btn @_cssClass"><span>@Text</span></button>
@code {
public enum Style
{
primary,
secondary,
tertiary,
danger
}
[Parameter]
public string Text { get; set; }
[Parameter]
public Style Apperance { get; set; }
private string _cssClass { get; set; }
protected override void OnInitialized()
{
_cssClass = Apperance switch
{
Style.secondary => "btn--secondary",
Style.tertiary => "btn--tertiary",
Style.danger => "btn--danger",
_ => "btn--primary"
};
}
}
我收到此错误:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
未处理的异常呈现组件:“Tidrapport.Client.Components.Button”类型的对象没有与名称“onclick”匹配的属性。
System.InvalidOperationException:“Tidrapport.Client.Components.Button”类型的对象没有与名称“onclick”匹配的属性。
我确实想要一个子按钮组件,当它被点击时(在父组件中呈现) - 我想在父组件中调用一个方法。
【问题讨论】:
标签: blazor