将默认参数应用于其他组件的组件 [英] Component which apply default parameters to other component
问题描述
我正在使用Blazorise,它提供带有多个参数的多个基本组件,例如< Button>
.我想创建一个简单的组件,该组件将呈现带有一个或两个硬编码参数(例如 Color ="Colors.Primary")的
然后将其余参数传递到< Button>
< Button>
.
I am using Blazorise, which provide multiple base components such as <Button>
, with multiple parameters.
I would like to create a simple component which would render a <Button>
with one or two parameters hard-coded (such as Color="Colors.Primary")
and then pass the rest of the parameters onto the <Button>
.
这是我想写的:
<PrimaryButton Outline>Text</Button>
我想实现此目的,而不必手动设置 PrimaryButton.razor
中< Button>
的所有可用参数.我一直在寻找一种方法来执行此操作,但是找不到任何东西.甚至有可能吗?
I would like to achieve this without having to manually set all parameters available from <Button>
in PrimaryButton.razor
. I have been looking for a way to do this for some time but can't find anything. Is It even possible?
我已经尝试从 Button
继承并编写< Button @attributes ="@@ Attributes"Color ="Colors.Primary"
,但隐式参数将被丢弃.
I already tried inheriting from Button
and writing <Button @attributes="@Attributes" Color="Colors.Primary">
but the implicit parameters are discarded.
推荐答案
我认为您仍然必须通过将它们添加到自定义组件中来将它们作为参数传递.例如,您可以执行设置了 Color
和 Outline
的按钮,但传递 Block
参数.
I think you still have to pass them as parameters by adding them to your custom component. For instance you could do a button with Color
and Outline
set but pass on the Block
parameter.
/* PrimaryButton.razor */
<Button Color="Color.Primary" Outline="true" Block="@Block">@ChildContent</Button>
@code {
[Parameter] public bool Block { get; set;}
[Parameter] public RenderFragment ChildContent { get; set; }
}
然后您可以将该新组件用作
You could then use that new component as
<PrimaryButton Block="true">Some Text</PrimaryButton>
这篇关于将默认参数应用于其他组件的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!