What's the difference between passing parameter values using @ or wrapped in quotes to a component in Blazor C#?

huangapple go评论72阅读模式
英文:

What's the difference between passing parameter values using @ or wrapped in quotes to a component in Blazor C#?

问题

  1. <MyButton Label="Some Label" />
  2. <MyButton Label=@someLabel />
  3. <MyButton Label="@someLabel" />
  4. <MyButton Label=someLabel />
  5. <MyButton @bind-Label=@someLabel />
  6. <MyButton @bind-Label="@someLabel" />
  7. <MyButton @Label="Some Label" />
英文:

I've been very confused about how to pass parameter values to C# Blazor components since there are several different syntaxes that are quite similar and I am unsure about when to use which.

For example, let's say there is a component

<MyButton />

that has a parameter property

[Parameter]
public string Label { get; set; }

What's the difference between

  1. <MyButton Label="Some Label" />
  2. <MyButton Label=@someLabel />
  3. <MyButton Label="@someLabel" />
  4. <MyButton Label=someLabel />
  5. <MyButton @bind-Label=@someLabel />
  6. <MyButton @bind-Label="@someLabel" />
  7. <MyButton @Label="Some Label" />

答案1

得分: 1

  1. 字面值: 这些是以字面值 string 形式传递给组件的值,作为简单的静态值。如果您的标签基本上不需要更改,而是具有固定的值,则这是合适的。
  2. 表达式: 表达式将某些本地变量(在这种情况下为 someLabel)分配给 Label 属性。这实际上意味着如果您的 someLabel 变量更改,组件将重新呈现,按钮标签将相应更新。因此,Label 属性具有动态值。这也称为单向绑定。
  3. 引用表达式: 使用 @ 符号或额外的引号进行动态分配没有区别。Blazor 允许带引号或不带引号的动态分配。
  4. 推断字面值: 乍一看,这可能看起来像是变量赋值。但是,Blazor 实际上将其推断为字面字符串,因为此语法在默认 HTML 中也是有效的。
  5. 双向绑定: 假设在 MyButton 中定义了对应的 [Parameter] EventCallback<T> LabelChanged,此语法双向绑定了变量 someLabel 到组件。这意味着不仅 someLabel 动态分配给组件标签,反过来也是如此。标签不是双向绑定的最佳用例,但在处理输入时,此行为确实非常有用。P. S.:为了使绑定正常工作,必须遵循命名约定 {属性名称}Changed 用于 EventCallback<T>
  6. 引用的双向绑定: 再次强调,Blazor 不关心您是否将分配包装在引号中。总结一下,此语法等同于5. 双向绑定。
  7. 指令: 指令,如 @ref@key@bind,为组件提供了额外的功能。然而,Blazor 不支持自定义指令(尚未支持)。由于无效的标记语法,Label 属性将不会被分配任何值(我假设是 null)。
英文:
  1. Literals: These are literal string values that are passed to the component as simple, static values. This is suitable if your label basically doesn't need to change but has a fixed value instead.
  2. Expressions: Expression assign some local variable (in that case someLabel to the Label property. Essentially this means that if your someLabel variable changes, the component will rerender and the button label will update accordingly. Thus the Label property has a dynamic value. This is also called One-Way Binding.
  3. Quoted Expressions: There is no difference between dynamic assignments with an @ sign only or with additional quotes. Blazor allows dynamic assignments with and without quotation mark wrappers.
  4. Inferred Literals: This might look like a variable assignment at first glance. However , Blazor actually infers this as a literal string since this syntax is valid in default html as well.
  5. Two-Way Binding: Suppose there is a corresponding [Parameter] EventCallback&lt;T&gt; LabelChanged defined in MyButton this syntax Two-Way binds a variable someLabel to the component. This means that not only is someLabel dynamically assigned to the component label but also the other way around. A label is not a great use case for Two-Way-Binding but this behavior is neat indeed when working with inputs for example. <br/> P. S.: It is necessary to follow the naming convention {name of property}Changed for the EventCallback&lt;T&gt; in order to make the binding work correctly.
  6. Quoted Two-Way Binding: Again, Blazor doesn't care whether you wrap an assignment in quotes or not. Concluding this syntax equals 5. Two-Way-Binding.
  7. Directives: Directives such as @ref, @key, or @bind bring additional functionality to a component. However, Blazor does not support custom directives (yet). The Label property won't be assigned to any value (I assume null) due to invalid markup syntax.

答案2

得分: 1

以下是翻译好的部分:

虽然你对所有7种情况的描述都是正确的,但这不是唯一的分类方式。

1 引号

Razor基于HTML,建议的做法是_始终_在参数值周围使用引号。而这些引号可以是单引号(&#39;)或双引号(&quot;)。
所以,不管其他方面如何,以下三种表示始终相同:

a. x=y
b. x=&#39;y&#39;
c. x=&quot;y&quot;

这里唯一的例外是,只有当值y中没有空格或其他无效字符时才允许使用表示法a)。

2 使用@前缀

Razor编译器知道参数的类型,并将接受C#值。唯一的例外是对于字符串,=“someLabel”=someLabel分配字面文本,但在=“@someLabel”中,它分配了string someLabel的内容。

你的示例

情况2)和3)完全相同,

情况1)和4)相同,1)需要引号,因为空格的缘故。

情况5)和6)再次完全相同。而且在这里@是可选的(Razor编译器知道无法将字面文本进行双向绑定),以下两者与5)和6)也相同:

  1. <MyButton @bind-Label=&#39;someLabel&#39; />
  2. <MyButton @bind-Label=&quot;someLabel&quot; />

来自文档

根据HTML5规范,大多数情况下,参数属性值周围的引号都是可选的。例如,Value=this得到支持,而不是Value="this"。然而,我们建议使用引号,因为这更容易记住,并在基于Web的技术中被广泛采用。

如果组件参数的类型是字符串,则默认情况下将属性值视为C#字符串字面量。如果要指定C#表达式,可以使用@前缀。

我们不建议在字面值(例如,布尔值),关键字(例如,this)或null等情况下使用@前缀,但如果你愿意,可以选择使用它们。例如,IsFixed="@true"是不常见但支持的。

英文:

While the description of all your 7 cases is correct, this is not the only way to categorize them.

1 Quotes

Razor is based on HTML and the recommended practice is to always use quotes around parameter values. And those quotes can be single (&#39;) or double (&quot;).
So, regardless of all other aspects, the following three notations are always the same:

a. x=y
b. x=&#39;y&#39;
c. x=&quot;y&quot;

The one exception here is that notation a) is only allowed when there are no spaces or other invalid chars in the value y.

2 prefix with @

The razor compiler knows the type of the parameter and will accept C# values here. The exception is with strings, =&quot;someLabel&quot; and =someLabel assign literal text but in =&quot;@someLabel&quot; it assigns the content of string someLabel.

Your examples

Cases 2) and 3) are exactly the same,

Cases 1) and 4) are the same , quotes are required for 1) because of the space.

Cases 5) and 6) are exactly the same again. And because the @ is optional here (the Razor compiler knows you can't 2-way bind to literal text), the following two are the same as 5) and 6) as well:

  1. &lt;MyButton @bind-Label=&#39;someLabel&#39; /&gt;
  2. &lt;MyButton @bind-Label=&quot;someLabel&quot; /&gt;

From the docs:

> Quotes around parameter attribute values are optional in most cases per the HTML5 specification. For example, Value=this is supported, instead of Value="this". However, we recommend using quotes because it's easier to remember and widely adopted across web-based technologies.

and

> If the component parameter is of type string, then the attribute value is instead treated as a C# string literal by default. If you want to specify a C# expression instead, then use the @ prefix.

> We don't recommend the use of the @ prefix for literals (for example, boolean values), keywords (for example, this), or null, but you can choose to use them if you wish. For example, IsFixed="@true" is uncommon but supported.

答案3

得分: 0

似乎许多程序员需要规则来感到舒适。简单来说,@符号用于从HTML切换到C#。如果在C#中不需要引号,则不要使用它们。Blazor .razor 文件是C#和HTML的组合。

英文:

Seems a lot of programmers need rules to feel comfortable. The simple way to look at this is the @ breaks from html to C#. If you don't need quotes in C# don't use them. Blazor .razor files are a combination of C# and html.

huangapple
  • 本文由 发表于 2023年7月10日 18:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定