0

Todat I am stuck with looping in Vue. I know the basics of loops and been looking around if these question are asked before. But none of them solved my problem.

Code loop:

<ul>
    <li :v-for="todo in todos">
       {{ todo.text }}
    </li>
</ul>

Code object:

todos: [
   { text: "Subscibe to Renatello.com newsletter", done: true },
   { text: "Learn Vue", done: false },
   { text: "Build awesome projects", done: false }
]

The error I get is: Cannot read property 'text' of undefined

Hopefully there is someone out here which can help me out.

SOLUTION:

The :v-for: as v-for="(todo, index) in todos" with an :key="index"

Thanks in advance.

3
  • 1
    your code object is wrong. Check the curly braces Commented Mar 29, 2021 at 8:32
  • Yea I see it now... but I had inserted those curly braces Commented Mar 29, 2021 at 8:33
  • 2
    Please update your question with a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button); here's how to do one and specifically, here's how to do one for Vue. Commented Mar 29, 2021 at 8:35

2 Answers 2

2

Remove the colon before the v-for.

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

Comments

1

I'm not strong on Vue, but I don't think you want the : on :v-for, just v-for:

const app = new Vue({
    el: '#app',
    data: {
        todos: [
            {
                text: "Subscibe to Renatello.com newsletter",
                done: true
            },
            {
                text: "Learn Vue",
                done: false
            },
            {
                text: "Build awesome projects",
                done: false
            }
        ]
    }
})

setTimeout(() => {
    app.todos = [
        ...app.todos,
        {
            text: "more",
            done: false
        }
    ];
}, 1000);
<div id="app">
    <ul>
        <li v-for="todo in todos">
            {{ todo.text }}
        </li>
    </ul>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

Comments

Your Answer

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