当前位置:网站首页>Blazor University (11)组件 — 替换子组件的属性
Blazor University (11)组件 — 替换子组件的属性
2022-04-23 03:02:00 【dotNET跨平台】
原文链接:https://blazor-university.com/components/replacing-attributes-on-child-components/
替换子组件的属性
源代码[1]
到目前为止,我们已经了解了如何创建代码生成的属性[2],以及如何捕获意外参数[3]。除了这两种技术之外,Blazor 还允许我们重写/替换子组件中的现有属性。
采取以下页面标记:
<ChildComponent first="consumer-value-1" second="consumer-value-2" />
它使用以下子组件:
<div @attributes=AllOtherAttributes>
Right-click and inspect the HTML for this element to see the results!
</div>
@code
{
[Parameter(CaptureUnmatchedValues=true)]
public Dictionary<string, object> AllOtherAttributes { get; set; }
}
正如我们之前在代码生成的属性[4]中看到的那样,ChildComponent 会将使用者提供的属性(第一个和第二个)捕获到我们的参数 AllOtherAttributes 中,并且对 @attributes=AllOtherAttributes 的调用将指示 Blazor 在 Dictionary<string, object> 中输出名称/值对。前面的代码将输出以下 HTML。
<div first="consumer-value-1" second="consumer-value-2">
Right-click and inspect the HTML for this element to see the results!
</div>
替换子属性
如果我们想为 first 和 second 指定默认值以在使用者不提供它们时输出怎么办?如果未设置值,则可能很容易重写 SetParametersAsync[5] 并插入值,但有一种更简单的方法!
我们所要做的就是写出我们的默认值作为子组件标记的一部分,@attributes= 指令将使用使用者传递的任何值覆盖它们。因此,如果我们更改子组件以指定一些默认属性值,如下所示:
<div first="1" second="2" third="3" fourth="4" @attributes=AllOtherAttributes>
Right-click and inspect the HTML for this element to see the results!
</div>
然后我们可以像这样替换组件的那些默认值:
<ChildComponent first="consumer-value-1" second="consumer-value-2" />
这将呈现以下 HTML:
<div first="consumer-value-1" second="consumer-value-2" third="3" fourth="4">
Right-click and inspect the HTML for this element to see the results!
</div>
我们的子组件将始终呈现其所有四个 HTML 属性,但也将允许使用者替换它们的值。
保护属性不被替换
在某些情况下,我们可能希望允许组件的使用者替换某些属性,但我们希望保护其他属性不被更改。例如:
<input class="form-control" type="number" @attributes=AllOtherAttributes />
在这个假设的 InputNumber 控件中,我们希望允许我们的使用者替换默认的 CSS 类属性,但不希望他们意外地将 type 从 number 更改为 checkbox。
在 Blazor 中,我们的 @attributes= 指令的位置很重要。指令之前的任何属性(在其上方或左侧)都可以由使用者替换其值,但在它之后(在其下方或右侧)的所有属性都受到保护,以免其值被替换。
鉴于以下标记:
<ChildComponent
first="consumer-value-1"
second="consumer-value-2"
inserted="consumer-inserted-value" />
然后调整 @attributes= 在我们的 ChildComponent 中的位置将为我们提供以下输出:
// Example 1
<div
@attributes=AllOtherAttributes
first="1"
second="2" />
// Generated HTML
<div
inserted="consumer-inserted-value
first="1"
second="2" />
// Example 2
<div
first="1"
@attributes=AllOtherAttributes
second="2" />
// Generated HTML
<div
first="consumer-value-1"
inserted="consumer-inserted-value
second="2" />
// Example 3
<div
first="1"
second="2"
@attributes=AllOtherAttributes />
// Generated HTML
<div
first="consumer-value-1"
second="consumer-value-2"
inserted="consumer-inserted-value />
R.I.P. 默认值
记住哪些值优先的一种简单方法是使用“R.I.P.方法”。
@attributes= 指令将始终插入来自使用者的附加值,因此将 I 视为插入的含义。I 之前的每个属性值都可以替换(R),I 之后的每个属性值都受保护(P)。
<div first="1" second="2" @attributes=AllOtherAttributes third="3" fourth="4" />

参考资料
[1]
源代码: https://blazor-university.com/components/replacing-attributes-on-child-components/
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124239638
边栏推荐
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- tf. keras. layers. Embedding function
- .NET7之MiniAPI(特别篇):.NET7 Preview3
- Navicat failed to connect to Oracle Database: cannot load OCI DLL, 87: instant client package is
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- eventBus
- Typescript Learning Guide
- Basic SQL (VIII) data update operation practice
- How to deploy a website with only a server and no domain name?
- Table space capacity query and expansion of Oracle Database
猜你喜欢

Distributed system services

REINFORCE

Linux redis - redis database caching service

Airtrack cracking wireless network password (Dictionary running method)

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

Q-Learning & Sarsa

C#中切片语法糖的使用

tf. keras. layers. MaxPooling? D function

Processes and threads

Er and eer models
随机推荐
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Response processing of openfeign
Summary of interface automation interview questions for software testing
JS relearning
《信息系統項目管理師總結》第六章 項目人力資源管理
Sonic cloud real machine tutorial
Blazor University (12)组件 — 组件生命周期
AspNetCore配置多环境log4net配置文件
[format] simple output (2)
Centos7 install MySQL 8 0
Detailed log display of openfeign call
C# WPF UI框架MahApps切换主题
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
tf. keras. layers. Embedding function
Cherno_ Game engine series tutorial (5): 101~
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
The express project changes the jade template to art template
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
When using art template inheritance, compileerror: invalid or unexpected token generated
SQL statement - DDL