0

I'm trying to add labels for inputs using v-for The inputs are working good(I'm creating them using v-for and it's creating them good), but When I'm trying to add labels to them the form becomes a big mess

*Integration Params is an object with key and value

This is the input creation:

<input v-for="(param, key, index) in integrationParams" v-model="integrationParams[key]" :id="key">

This is the label creation:

<label class="inputLabel" v-for="(labelFor, key, index) in integrationParams" :for="key">Please fill in {{ key }} param</label>

It actually does create 5 labels for example, but it comes out like this:

form

2 Answers 2

1

You need to pair them inside a parent div

<div v-for="(param, key, index) in integrationParams" :key="key">
    <input v-model="param" :id="key">
    <label class="inputLabel" :for="key">Please fill in {{ key }} param</label>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

VueCompilerError: v-model cannot be used on v-for or v-slot scope variables because they are not writable. This is what I get :(
try v-model="integrationParams[key]"
@OsherRevach Then use the accessor of the object. v-model="integrationParams[key]". Something is writable, you're just not targeting the correct property.
0

this is you solution:

<script src="https://unpkg.com/vue"></script>

<div id="app">
  <p>{{ message }}</p>
  <div v-for="item in integrationParams" :key="item.message" >
<label> {{ item.message }} </label>
<input  v-model="item.message" :id="item">

</div>
</div>

and the vue code:

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!',
    integrationParams: [
      { message: 'Foo' },
      { message: 'Bar' }
    ]
    
  }
})

Demo: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.