我的组件中有一个道具:
props: {
form_data: {
type: Object,
required: true
},
...
如果在form_data
中有一个discount
的字段,则它是一个字符串。
但现在在我的组件中我想将折扣绑定为Number。
<NumberInput v-model: form_data.discount />
但在<NumberInput>
中form_data.discount
应为Number not String。在我的方式中,我将设置另一个computed
parma来转换form_data.discount
,如下所示:
computed: {
edit_form_data: {
id: this.form_data.id,
...
belong_product: this.form_data.belong_product
}
}
然后我使用edit_form_data
绑定模板。
但我觉得这很麻烦,不优雅。谁能告诉我是否有其他方法可以实现这一目标?比如过滤器等等。
答案 0 :(得分:0)
如果所有项目form_data.discount
都可以作为整数使用而没有问题,您可以在按以下方式对其进行转换时进行转换:
form_data.discount = Number(form_data.discount);
否则在<<>>内部转换组件