Open
Description
Vue version
3.4.25
Link to minimal reproduction
Steps to reproduce
- declare radio buttons with values that can be parsed as number
- add a
ref()
and bind the radio buttons withv-model.number
to it
What is expected?
The value of the ref
should be of type number
.
What is actually happening?
It is always of type string
(tested in Chrome and Firefox.)
System Info
No response
Any additional comments?
No response
Activity
skirtles-code commentedon May 8, 2024
I believe the
trim
modifier has the same problem. Modifiers also seem to be ignored fortype="checkbox"
.It seems relatively easy to work around this problem. I wonder whether it's worth the extra bytes to support it. That said, if it isn't supported that should probably be documented somewhere.
YiMo1 commentedon May 9, 2024
Yes, in the source code of the
v-model
, there is no modifier judgment made for buttons of types checkbox and radio. If Vue's official intention is to do so, then I think it should be explained in the document.neneodonkor commentedon May 11, 2024
From the docs it says:
.number
cast valid input string to numbers.I think it means strings that are being typed into an input or text box. With the radio button you are not inputing any text. You are just changing the state of the radio button from checked to unchecked. And you have an attribute with the name value, which will always be in string. The value attribute is just a way to identify or describe the state of the radio button depending on the context. So, if I want to use on and off, or true and false, these are just descriptive values.
Perhaps, a workaround is to bind the value attribute to a constant variable of number type.
:value="num1"
whereconst num1 = 1
.Or an object can be used in this case:
YiMo1 commentedon May 21, 2024
我认为 @neneodonkor 说的很对,所以,我觉得这个问题严格来说不算是一个问题,如果想对
radio
元素使用.number
,那为什么不直接使用:value="number"
呢?所以我认为正因如此,Vue官方才只对
input
元素做了这些修饰符neneodonkor commentedon May 21, 2024
@YiMo1 感謝