admin管理员组文章数量:1316974
I am using <q-select>
ponent and inside that i'm sending options that i fetch from API, as value I set id of object, but problem is that it expects string to get and ID is a number and because of that I'm getting error.
Is it possible to change type of data inside v-model
.
<s-select
autoplete
sorted
v-model="data.id"
options="list"
option-value="value"
option-label="label"
label="Field"
required
/>
I've tried to put data.id.toString()
inside v-model but then I got error.
How can i resolve this?
I am using <q-select>
ponent and inside that i'm sending options that i fetch from API, as value I set id of object, but problem is that it expects string to get and ID is a number and because of that I'm getting error.
Is it possible to change type of data inside v-model
.
<s-select
autoplete
sorted
v-model="data.id"
options="list"
option-value="value"
option-label="label"
label="Field"
required
/>
I've tried to put data.id.toString()
inside v-model but then I got error.
How can i resolve this?
Share Improve this question asked May 10, 2020 at 17:41 kavkaxkavkax 953 gold badges4 silver badges10 bronze badges4 Answers
Reset to default 3You can use a puted method
with a getter/setter defined.
puted: {
putedDataId: {
get() {
return this.data.id.toString()
},
set(val) {
this.data.id = Number(val)
}
}
}
And then use the puted method as the model
<s-select
autoplete
sorted
v-model="putedDataId"
options="list"
option-value="value"
option-label="label"
label="Field"
required
/>
You can also consider converting data.id
to a string when getting it.
You cannot apply the method to v-model
. One solution would be converting the id
to string
directly after fetching the data as follows:
let id = data.id;
data.id = id.toString();
<s-select
autoplete
sorted
:value="data.id.toString()"
options="list"
option-value="value"
option-label="label"
label="Field"
required
/>
I suggest using a puted property for that
puted: {
id: {
return String(this.data.id);
}
}
Your template will be cleaner and stays reactive
<s-select
autoplete
sorted
v-model="id"
options="list"
option-value="value"
option-label="label"
label="Field"
required
/>
本文标签: javascriptConvert vmodel value from number to stringStack Overflow
版权声明:本文标题:javascript - Convert v-model value from number to string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742019761a2414435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论