当前位置:网站首页>Blazor University (11) component - replace attributes of subcomponents
Blazor University (11) component - replace attributes of subcomponents
2022-04-23 03:06:00 【Dotnet cross platform】
Link to the original text :https://blazor-university.com/components/replacing-attributes-on-child-components/
Replace the properties of the subcomponent
Source code [1]
up to now , We've seen how to create code generated properties [2], And how to catch unexpected parameters [3]. In addition to these two technologies ,Blazor It also allows us to rewrite / Replace existing properties in subcomponents .
Take the following page tags :
<ChildComponent first="consumer-value-1" second="consumer-value-2" />
It uses the following subcomponents :
<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; }
}
Just like the properties we generated in the code [4] As seen in ,ChildComponent The properties provided by the user ( The first and the second ) Capture our parameters AllOtherAttributes in , And right @attributes=AllOtherAttributes The call of will indicate Blazor stay Dictionary<string, object> Output name in / It's worth it . The previous code will output the following HTML.
<div first="consumer-value-1" second="consumer-value-2">
Right-click and inspect the HTML for this element to see the results!
</div>
Replace sub attribute
If we want to help first and second Specify default values to output what to do if the user does not provide them ? If no value is set , It may be easy to rewrite SetParametersAsync[5] And insert the value , But there's a simpler way !
All we have to do is write out our default value as part of the sub component tag ,@attributes= The instruction will overwrite them with any value passed by the user . therefore , If we change the subcomponent to specify some default attribute values , As shown below :
<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>
Then we can replace the default values of the component like this :
<ChildComponent first="consumer-value-1" second="consumer-value-2" />
This will present the following 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>
Our subcomponent will always render all four of its HTML attribute , But it will also allow users to replace their values .
Protection properties are not replaced
In some cases , We may want to allow the user of the component to replace some properties , But we want to protect other properties from being changed . for example :
<input class="form-control" type="number" @attributes=AllOtherAttributes />
In this hypothetical InputNumber Control , We want to allow our users to replace the default CSS Class properties , But I don't want them to accidentally type from number Change to checkbox.
stay Blazor in , our @attributes= The position of the command is very important . Any attribute before the instruction ( Above or to the left ) Can be replaced by the user , But after it ( Below or to the right ) All properties of are protected , So that its value is not replaced .
In view of the following marks :
<ChildComponent
first="consumer-value-1"
second="consumer-value-2"
inserted="consumer-inserted-value" />
Then adjust @attributes= In our ChildComponent The location in will provide us with the following output :
// 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. The default value is
A simple way to remember which values take precedence is to use “R.I.P. Method ”.
@attributes= The directive will always insert additional value from the user , So it will I The meaning of deemed insertion .I Every previous attribute value can be replaced (R),I Every subsequent attribute value is protected (P).
<div first="1" second="2" @attributes=AllOtherAttributes third="3" fourth="4" />

Reference material
[1]
Source code : https://blazor-university.com/components/replacing-attributes-on-child-components/
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334687.html
边栏推荐
- The space between the left and right of the movie ticket seats is empty and cannot be selected
- 最通俗易懂的依赖注入与控制反转
- C#中元组对象Tuple的使用
- Face longitude:
- Tips in MATLAB
- Detailed log display of openfeign call
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
- C# 11 的这个新特性,我愿称之最强!
- Plug in for vscode
猜你喜欢

Detailed explanation of distributed things

Plug in for vscode

Onenet connection process

Q-Learning & Sarsa

Deep q-network (dqn)

Blazor University (11)组件 — 替换子组件的属性

tf. keras. layers. Timedistributed function

Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)

TP5 customization in extend directory succeeded and failed. Return information

tf. keras. layers. Embedding function
随机推荐
Detailed explanation of distributed things
Use of MySQL command line client and common commands
Onenet connection process
Chapter VI project information management system summary
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
.NET7之MiniAPI(特别篇):.NET7 Preview3
HLS / chisel practice CORDIC high performance computing complex square root
[software testing] understand the basic knowledge of software testing
Passing object type parameters through openfeign
微软是如何解决 PC 端程序多开问题的——内部实现
C# 读写二进制文件
Regular object type conversion tool - Common DOM class
Load view Caton
TP5 inherits base and uses the variables in base
Distributed system services
Introduction and use of openfeign component
Numpy append function
Laravel's own paging query
Middle and rear binary tree
tf. keras. layers. Embedding function