网站建设服务费应该算什么科目,研究院网站建设,欧美一级a做爰片免费网站,天津智能网站建设多少钱原文链接#xff1a;https://blazor-university.com/components/replacing-attributes-on-child-components/替换子组件的属性源代码[1]到目前为止#xff0c;我们已经了解了如何创建代码生成的属性[2]#xff0c;以及如何捕获意外参数[3]。除了这两种技术之外#xff0c;B… 原文链接https://blazor-university.com/components/replacing-attributes-on-child-components/替换子组件的属性源代码[1]到目前为止我们已经了解了如何创建代码生成的属性[2]以及如何捕获意外参数[3]。除了这两种技术之外Blazor 还允许我们重写/替换子组件中的现有属性。采取以下页面标记ChildComponent firstconsumer-value-1 secondconsumer-value-2 /它使用以下子组件div attributesAllOtherAttributesRight-click and inspect the HTML for this element to see the results!
/divcode
{[Parameter(CaptureUnmatchedValuestrue)]public Dictionarystring, object AllOtherAttributes { get; set; }
}正如我们之前在代码生成的属性[4]中看到的那样ChildComponent 会将使用者提供的属性第一个和第二个捕获到我们的参数 AllOtherAttributes 中并且对 attributesAllOtherAttributes 的调用将指示 Blazor 在 Dictionarystring, object 中输出名称/值对。前面的代码将输出以下 HTML。div firstconsumer-value-1 secondconsumer-value-2Right-click and inspect the HTML for this element to see the results!
/div替换子属性如果我们想为 first 和 second 指定默认值以在使用者不提供它们时输出怎么办如果未设置值则可能很容易重写 SetParametersAsync[5] 并插入值但有一种更简单的方法我们所要做的就是写出我们的默认值作为子组件标记的一部分attributes 指令将使用使用者传递的任何值覆盖它们。因此如果我们更改子组件以指定一些默认属性值如下所示div first1 second2 third3 fourth4 attributesAllOtherAttributesRight-click and inspect the HTML for this element to see the results!
/div然后我们可以像这样替换组件的那些默认值ChildComponent firstconsumer-value-1 secondconsumer-value-2 /这将呈现以下 HTMLdiv firstconsumer-value-1 secondconsumer-value-2 third3 fourth4Right-click and inspect the HTML for this element to see the results!
/div我们的子组件将始终呈现其所有四个 HTML 属性但也将允许使用者替换它们的值。保护属性不被替换在某些情况下我们可能希望允许组件的使用者替换某些属性但我们希望保护其他属性不被更改。例如input classform-control typenumber attributesAllOtherAttributes /在这个假设的 InputNumber 控件中我们希望允许我们的使用者替换默认的 CSS 类属性但不希望他们意外地将 type 从 number 更改为 checkbox。在 Blazor 中我们的 attributes 指令的位置很重要。指令之前的任何属性在其上方或左侧都可以由使用者替换其值但在它之后在其下方或右侧的所有属性都受到保护以免其值被替换。鉴于以下标记ChildComponentfirstconsumer-value-1secondconsumer-value-2insertedconsumer-inserted-value /然后调整 attributes 在我们的 ChildComponent 中的位置将为我们提供以下输出// Example 1
divattributesAllOtherAttributesfirst1second2 /// Generated HTML
divinsertedconsumer-inserted-valuefirst1second2 /// Example 2
divfirst1attributesAllOtherAttributessecond2 /// Generated HTML
divfirstconsumer-value-1insertedconsumer-inserted-valuesecond2 /// Example 3
divfirst1second2attributesAllOtherAttributes /// Generated HTML
divfirstconsumer-value-1secondconsumer-value-2insertedconsumer-inserted-value /R.I.P. 默认值记住哪些值优先的一种简单方法是使用“R.I.P.方法”。attributes 指令将始终插入来自使用者的附加值因此将 I 视为插入的含义。I 之前的每个属性值都可以替换(R)I 之后的每个属性值都受保护(P)。div first1 second2 attributesAllOtherAttributes third3 fourth4 /参考资料[1]源代码: https://blazor-university.com/components/replacing-attributes-on-child-components/