简体   繁体   中英

Generate valid v-model value using dot notation string as object reference to the data

Basically i've made proyxy-component which renders different components based on what the :type is and it works great. The point is that I create a schema of the form controls and a separate data object where the data from the form controls is stored. Everything is working good but i have a problem when formData object contains nested objects.

In my example test.test1

How can i make the v-model value dynamic which is generated based on what the string is.

My Compoennt

<proxy-component
                v-for="(scheme, index) in personSchema.list"
                :key="index"
                :type="scheme.type"
                :props="scheme.props"
                v-model="formData[personSchema.prefix][scheme.model]"
                v-validate="'required'"
                data-vv-value-path="innerValue"
                :data-vv-name="scheme.model"
                :error-txt="errors.first(scheme.model)"
></proxy-component>

Data

data() {
            return {
                selectOptions,
                formData: {
                    person: {
                        given_names: '',
                        surname: '',
                        sex: '',
                        title: '',
                        date_of_birth: '',
                        place_of_birth: '',
                        nationality: '',
                        country_of_residence: '',
                        acting_as: '',
                        test: {
                            test1: 'test',
                        },
                    },
                },
                personSchema: {
                    prefix: 'person',
                    list: [
                        {
                            model: 'given_names',
                            type: 'custom-input-component',
                            props: {
                                title: 'Name',
                            },
                        },
                        {
                            model: 'surname',
                            type: 'custom-input-componentt',
                            props: {
                                title: 'Surname',
                            },
                        },
                        {
                            model: 'test.test1',
                            type: 'custom-input-component',
                            props: {
                                title: 'test 1',
                            },
                        },
                        {
                            model: 'sex',
                            type: 'custom-select-component',
                            props: {
                                title: 'Sex',
                                options: selectOptions.SEX_TYPES,
                                trackBy: 'value',
                                label: 'name',
                            },
                        },
                    ],
                },

            };
        },

I don't think it's possible directly with v-model, you can take a look at:

https://v2.vuejs.org/v2/guide/reactivity.html

Maybe the best solution would be use a watch ( deep: true ) as a workaround. (I would try first to use watch properties inside formData[personSchema.prefix][scheme.model] .)

I would recomment you to write a vue-method (under the data section) that returns the object for v-model

v-model="resolveObject(formData[personSchema.prefix][scheme.model])" or v-model="resolveObject([personSchema.prefix] , [scheme.model])"

There you can do handle the dot-notation and return the proper nested property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM