Question 18
Given the following parent-child component setup:
Parent Component: App.vue
<template> <div> <h2>Selected Tasks</h2> <task-list :tasks="tasks" @task-selected="addToSelection" /> <p>Total Selected: {{ selectedTasks.length }}</p> <ul> <li v-for="task in selectedTasks" :key="task.id">{{ task.name }}</li> </ul> </div></template>
<script>import TaskList from './TaskList.vue';
export default { components: { TaskList }, data() { return { tasks: [ { id: 1, name: 'Design UI' }, { id: 2, name: 'Write Backend' }, { id: 3, name: 'Write Tests' } ], selectedTasks: [] };
}, methods: { addToSelection(task) { const exists = this.selectedTasks.some(t => t.id === task.id); if (!exists) { this.selectedTasks.push(task); } } }};</script>Child Component: TaskList.vue
<template> <div> <h3>All Tasks</h3> <ul> <li v-for="task in tasks" :key="task.id"> {{ task.name }} <button @click="select(task)">Select</button> </li> </ul> </div></template>
<script>export default { props: ['tasks'], methods: { select(task) { this.$emit('task-selected', task); } }};</script>Test Scenario
- The app loads.
- User clicks "Select" on Design UI and Write Backend in order.
- User clicks "Select" on Design UI again.
Based on the above data, answer the given subquestions.
What happens when the user clicks "Select" on Design UI again (step 3)?
It is added again, making the total 3
It replaces the previous task
Nothing changes, no duplicate is added
Vue throws a warning about duplicate keys