Answer the question
In order to leave comments, you need to log in
What is the best way to organize data transfer in Vue?
Hello.
There are, for example,
[
{
"id": 0,
"title": "Задача №1",
"description": "Описание задачи",
"variants": [
{
"variantId": 0,
"text": "Текст первого варианта",
"isMarked": true
},
{
"variantId": 1,
"text": "Текст второго варианта",
"isMarked": false
}
]
},
{
"id": 1,
"title": "Задача №2",
"description": "Описание задачи",
"variants": [
{
"variantId": 0,
"text": "Текст первого варианта",
"isMarked": true
},
{
"variantId": 1,
"text": "Текст второго варианта",
"isMarked": true
},
{
"variantId": 2,
"text": "Текст третьего варианта",
"isMarked": true
}
]
},
{
"id": 2,
"title": "Задача №3",
"description": "Описание задачи",
"variants": [
{
"variantId": 0,
"text": "Текст первого варианта",
"isMarked": false
},
{
"variantId": 1,
"text": "Текст второго варианта",
"isMarked": false
},
{
"variantId": 2,
"text": "Текст третьего варианта",
"isMarked": false
},
{
"variantId": 3,
"text": "Текст четвертого варианта",
"isMarked": true
}
]
}
]
isMarked
can be reversed. // index.vue
<template>
<div class="task-list">
<Task v-for="data in tasks" :key="data.id" :data="data" />
</div>
</template>
<script>
// ...
data() {
return {
tasks: [/* массив из примера выше*/]
}
}
</script>
// Task.vue
<template>
<div>
<h1>{{ data.title }}</h1>
<p>{{ data.description }}</p>
<ValiantsList :variants="data.variants" />
</div>
</template>
<script>
// ...
props: {
data: {
type: Object,
required: true,
}
}
</script>
// variantsList.vue
<template>
<div>
<Variant v-for="variant in variants" :key="variant.id" :variant="variant" />
</div>
</template>
// variant.vue
<template>
<div>
<p>{{ variant.text }}</p>
<span @click="toggleMark">{{ markLabel }}</span>
</div>
</template>
<script>
// ...
computed: {
markLabel() {
return variant.isMarked ? 'Снять отметку' : 'Поставить отметку'
}
},
methods: {
toggleMark() {
// ???
}
}
</script>
isMarked
clicked option API. If the values variantId
were unique for any questions, then, in the case of Vuex, it would be possible to get out: search in all tasks and in them for all answer options, with which variantId matched, and change isMarked. But in this example, this will not work, because there is not enough id from the task itself, to facilitate searching in Vuex. taskId
so as not to drag it as a separate property through nested components, but this is somehow so-sovariantsList
and emit an event from variant
catching it in task
. But there can be many intermediate components. Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question