Reputation: 1
v-model value inside v-for loop is not unique.
This is my template:
<template>
<div
id="FAQ"
v-for="(question, index) in questions.slice().reverse()"
:key="index"
class="m-2 col-7"
>
<div v-if="getloggedUser.role == 'tutor'">
<div id="divQuestion">
<p class="m-5">{{ question.text }}</p>
</div>
<div id="divAnswer" class="mt-2">
<p class="m-2">{{ question.answer }}</p>
</div>
</div>
<div v-else>
<div id="divQuestion">
<p class="m-5">{{ question.text }}</p>
</div>
<div id="divAnswer" class="mt-2">
<p class="m-2">{{ question.answer }}</p>
</div>
<div v-if="question.answer == null" id="divPsycAnswer">
<textarea cols="60" rows="3" style="border-radius:12px" v-model="answer.text"></textarea>
<button id="btnSend" @click="publishAnswer(question.id)">Responder</button>
</div>
</div>
</div>
</template>
This is the script (data):
data() {
return {
// arrayFAQ: [],
form: {
text: ""
},
answer: {
text: ''
},
questions: [],
message: "",
loading: false,
id: null
}
},
This way, everytime I write in one textarea, I write for all the others. I have tried adding [index] in the v-model but there's this error: "TypeError: Cannot use 'in' operator to search for '0' in "
Upvotes: 0
Views: 414
Reputation: 459
Try this as your v-model
<textarea v-model="answers[`text${index}`]"></textarea>
The answer object could be an empty object. It will give you text0, text1, text2, as answer's properties. You can add anything instead of index, like your question's id for easy access and knowing which text is from which question.
Upvotes: 2