0

Can somebody please tell me how I have to write the "doneChange" method that I can access on the individual index of the tasks list to change the boolean of finish from true to false and viceversa?

My Code:

<template>
<div>
<h1>Alle Aufgaben</h1>
        <ul>
            <li v-for="(task, index) in tasks" :class="{ 'done' : task.finish}">
                <p>{{task.description}}</p>
                <button class="doneChange" @click="doneChange(index)">✓</button>
                <button class="del" @click="del(index)">X</button>
            </li>
        </ul>
</div>
</template>

<script>
import { ref, reactive, computed } from "vue";
export default {
  setup() {
    let tasks = [
            {description: "Frühstücken", finish: true},
            {description: "Lernen", finish: false},
            {description: "Trainieren", finish: false},
            {description: "Einkaufen", finish: false},
            {description: "Mails", finish: false},
            {description: "Abendessen", finish: false},
        ];

    const del = (index) => this.tasks.splice(index,1);
    const doneChange = (index) => tasks(index).finish = !tasks(index).finish;

        return { tasks, del, doneChange };
    }
};
</script>

With this I get the error:

Uncaught TypeError: tasks is not a function

1 Answer 1

0

In JavaScript, the parentheses after a symbol invokes a function:

tasks(index).finish = !tasks(index).finish  // ❌ calls a function named `tasks`
     ^     ^                ^     ^

Instead of parentheses, use square brackets to reference an array element by index:

tasks[index].finish = !tasks[index].finish

Also tasks should be a reactive prop so that its changes causes a re-render:

let tasks = reactive([/*...*/])

demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.