5

For example: <component v-model='foo' :is='boo' ...>.

foo's value stays the same during input.

I'm trying to solve the issue for quite a long time. I've checked lots of questions and threads but none of those helped me.

HTML doesn't work:

            <component
                :is="field.component"
                :key="key"
                :name="field.name"
                v-for="(field, key) in integration_data"
                v-model="field.value"
            >
            </component>

HTML works fine:

            <input
                :key="key"
                :name="field.name"
                v-for="(field, key) in integration_data"
                v-model="field.value"
            >

Vue controller:

export default {
init: function (init_data) {

    return new Vue({
        data: {
            integration_data: [
              {name: 'field_name0', component: 'input', value: ''},
              {name: 'field_name0', component: 'input', value: ''},
            ]
        },
    });
}
}
BugDeveloper
  • 155
  • 2
  • 2
  • 11

2 Answers2

13

You can't use input as a type of component and expect it to be a native input element. :is must name a component (which can contain an input, if you want).

Then you have to understand how v-model works on components:

So for a component to work with v-model, it should (these can be configured in 2.2.0+):

  • accept a value prop
  • emit an input event with the new value

Putting that all together, you can use v-model with :is.

new Vue({
  el: '#app',
  data: {
    integration_data: [{
      name: 'one',
      component: 'one',
      value: 'ok'
    }]
  },
  components: {
    one: {
      props: ['name', 'value'],
      template: '<div>{{name}} and <input v-model="proxyValue"><slot></slot></div>',
      computed: {
        proxyValue: {
          get() { return this.value; },
          set(newValue) { this.$emit('input', newValue); }
        }
      }
    }
  }
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.3.4/vue.min.js"></script>
<div id="app">
  <component 
    :is="field.component" 
    v-for="(field, key) in integration_data" 
    :key="key" 
    :name="field.name" 
    v-model="field.value"
  >
    <div>{{field.value}}</div>
  </component>
</div>
tony19
  • 125,647
  • 18
  • 229
  • 307
Roy J
  • 42,522
  • 10
  • 78
  • 102
-2

v-model has nothing to do with what you're possibly trying to do. It looks like you are trying to insert components dynamically. That's exactly what :is does. Now, to pass data to the component, you should use props.

For example:

Your Vue instance:

const vm = new Vue({
  el: '#app',
  data: {
    exampleValue: 'This value will be passed to the example component'
  }
})

Register a component:

Vue.component('example-component', {
  // declare the props
  props: ['value'],
  template: '<span>{{ value}}</span>'
})

Then use it like this:

<example-component :value="exampleValue"></example-component>

Or:

<component :is="'example-component'" :value="exampleValue"></component>
Ikbel
  • 7,721
  • 3
  • 34
  • 45
  • Thank you for the reply. I wanted not to just to pass data to a component, but to allow the component make changes to it. On Vue's github issues it has been told that this is not possible with native elements. – BugDeveloper Jun 30 '17 at 17:54