e-schultz / vue-dynamic-components

Exploring Dynamic Components with Vue
77 stars 21 forks source link

handling different types for value #4

Closed mispp closed 4 years ago

mispp commented 5 years ago

what would be the best way to handle different types of 'value' when you want to JSON.stringify(formData)?

what i want:

{
  "name": "test",
  "rate": 30
}

what i get:

{
  "name": "test",
  "rate": "30"
}

i've solved it like this, but this seems suboptimal.

    updateForm(fieldName: string, value: any, fieldType: string) {
            // console.log(this.formData);
            if (fieldType === 'FormGeneratorInputString') {
                this.$set(this.formData, fieldName, value);
            } else {
                this.$set(this.formData, fieldName, Number(value));
            }

            this.$emit('input', this.formData);
        },

any way to better bind types when using TypeScript?

PS. Awesome tutorial.