Blazor 的 InputBase<T> 中的 CurrentValue 和 Value 属性有什么区别?
What is the difference between CurrentValue and Value properties inside the Blazor's InputBase<T>?
查看 Microsoft 文档 here 它说明如下:
CurrentValue -> 获取或设置输入的当前值。
Value -> 获取或设置输入的值。这应该与双向绑定一起使用。
这告诉我的不多。是否有单独定义这些属性的原因?如果是,为什么?无论组件生命周期处于哪个阶段,它们是否始终包含相同的值?如果是这样,为什么要有 2 个不同的属性?在 Internet 上的各种示例中,我看到人们声明对这两个属性进行绑定。我错过了什么?
Is there a reason these properties are defined separately and if so why?
是的,Value
是一个 public Parameter
,这意味着 Blazor 代码将保持其值 - 每当它呈现时设置它。组件内的代码 不应 不直接设置此值(尽管它确实设置了),而是应该 Invoke
ValueChanged
EventCallback。这是通过 CurrentValue
和 CurrentValueAsString
属性在 InputBase
中处理的。
Do they always contain the same value regardless of the component lifecycle stage?
CurrentValue
不“包含”一个值 - 但是 getter
returns Value
,所以如果你正在阅读它们,它们引用相同的值.
why have 2 different properties at all?
参见答案 1 - public
Parameters
由 Blazor 内部代码维护,不应直接修改。但实际上,他们在这里无论如何都违反了这条规则,所以真正的原因是他们可以在输入值发生变化时调用其他代码——在 CurrentValueAsString
(解析)和 [=] 的 setter
中15=](验证)
In various examples on the Internet I've seen people declare binding with both properties. What am I missing?
用户代码只应 @bind
到 Input???
组件的 public Value
(@bind-Value
)。
如果您要实现继承 InputBase
的新 InputFoo
组件,则您的代码应使用 CurrentValue
/CurrentValueAsString
绑定到 DOM 元素.
查看 Microsoft 文档 here 它说明如下:
CurrentValue -> 获取或设置输入的当前值。
Value -> 获取或设置输入的值。这应该与双向绑定一起使用。
这告诉我的不多。是否有单独定义这些属性的原因?如果是,为什么?无论组件生命周期处于哪个阶段,它们是否始终包含相同的值?如果是这样,为什么要有 2 个不同的属性?在 Internet 上的各种示例中,我看到人们声明对这两个属性进行绑定。我错过了什么?
Is there a reason these properties are defined separately and if so why?
是的,Value
是一个 public Parameter
,这意味着 Blazor 代码将保持其值 - 每当它呈现时设置它。组件内的代码 不应 不直接设置此值(尽管它确实设置了),而是应该 Invoke
ValueChanged
EventCallback。这是通过 CurrentValue
和 CurrentValueAsString
属性在 InputBase
中处理的。
Do they always contain the same value regardless of the component lifecycle stage?
CurrentValue
不“包含”一个值 - 但是 getter
returns Value
,所以如果你正在阅读它们,它们引用相同的值.
why have 2 different properties at all?
参见答案 1 - public
Parameters
由 Blazor 内部代码维护,不应直接修改。但实际上,他们在这里无论如何都违反了这条规则,所以真正的原因是他们可以在输入值发生变化时调用其他代码——在 CurrentValueAsString
(解析)和 [=] 的 setter
中15=](验证)
In various examples on the Internet I've seen people declare binding with both properties. What am I missing?
用户代码只应 @bind
到 Input???
组件的 public Value
(@bind-Value
)。
如果您要实现继承 InputBase
的新 InputFoo
组件,则您的代码应使用 CurrentValue
/CurrentValueAsString
绑定到 DOM 元素.