0

I am working with Vue.js within the Quasar framework to develop a hybrid app. At one point, I have a somewhat large form that I have split into subcomponents to deal with sections of input, and I’m using two-way binding via the .sync modifier (https://v2.vuejs.org/v2/guide/components.html#sync-Modifier, available in Vue 2.3.0+) to handle the data. I am then using computed properties to get and set each value in the parent. For example:

parent: 
//html template
<parent>
  <child :firstName.sync="required.firstName"></child>
</parent>

child: 
//js
computed {
  sync_firstName: {
    get () {
      return this.firstName
    },
    set (val) {
      this.$emit('update:firstName', val)
     }
   }
 }

This works well when I hardcode the v-model directive, as in <input type=“text” v-model=“sync_firstName”/> . However, when I assign the v-model attribute with a v-for directive (using :key and dynamically rendered :options), the model value is read but the computed property not accessed/evaluated, for instance:

<div v-for=“option in data>
  <q-select v-model=“data[’name’]” :options=“data.options"></q-select>
</div>

According to the eslint plugin guide for v-model (https://github.com/vuejs/eslint-plugin-vue/blob/master/docs/rules/valid-v-model.md), it is possible to use v-for to establish v-model properties. In my case, rendering the data {{ data.name }} shows the correct value (sync_firstName) — but this property is not then being evaluated (though the options values are being correctly rendered). However, if I hardcode the computed property <q-select v-model=“sync_firstName”></q-select>, the field renders perfectly. What am I missing?

Thanks!

tony19
  • 125,647
  • 18
  • 229
  • 307
naomi13
  • 1
  • 2
  • if you use watch instead of computed, does it work? – V. Sambor Jan 26 '18 at 20:50
  • Just played around with that, doesn't seem to be able to understanding getters and setters within a loop. The watcher again works for the hardcoded input field but doesn't seem to be getting the prop values within the loop. – naomi13 Jan 27 '18 at 20:37
  • Update: I got the loop with v-select to work by making sure I wasn't confusing the variable name for the computed property with the computed property itself. Thanks for the direction, @V.Sambor! – naomi13 Jan 27 '18 at 23:06
  • You're welcome. Glad it works :) – V. Sambor Jan 28 '18 at 12:08

0 Answers0