转换组件中的道具数据字段

时间:2018-04-11 09:31:12

标签: vue.js

我的组件中有一个道具:

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绑定模板。

但我觉得这很麻烦,不优雅。谁能告诉我是否有其他方法可以实现这一目标?比如过滤器等等。

1 个答案:

答案 0 :(得分:0)

如果所有项目form_data.discount都可以作为整数使用而没有问题,您可以在按以下方式对其进行转换时进行转换:

form_data.discount = Number(form_data.discount);

否则在<<>>内部转换组件