0

I am trying to implement a recursive component, which can be exemplified as a recursive todo list. In the root component I can add new todos. A todo may have its own sub todos.

The Todo class

export class Todo {
    constructor(text) {
        this.id = Math.random()
        this.text = text
        this.todos = []
    }
}

Root component

<template>
    <div class="w-full px-3 py-3">
        <todo-card v-for="todo in todos" v-bind:key="todo.id"
            v-bind:todo="todo"
        ></todo-card>
        <div class="m-4 mt-8 p-4 border-t-2 border-gray-300">
            <i @click="addTodo" class='p-2 text-gray-400 fa fa-plus' title='Add Todo'></i>
        </div>
    </div>
</template>

<script>

export default {

    data: function () {
        return {
            todos: [
                new Todo('Make a call'),
                new Todo('Clean the house'),
            ]
        }
    },

    methods: {
        addTodo: function(e) {
            this.todos.push(new Todo('No name yet'))
        }        
    }
}
</script>

Todo Card component

<template>
    <div class="m-4 px-4 py-2 border-dashed border-2 border-gray-300 bg-blue-100">
        <div class="flex justify-between">
            <span class="text-sm text-gray-400"> {{todo.text}}</span>
        </div>
        <todo-card
            v-for="todo in todo.todos"
            v-bind:key="todo.id"
            v-bind:todo="todo"
        ></todo-card>       

        <div class="m-4 pt-4">
            <i @click="addTodo" class='text-gray-400 fa fa-plus' title='Add Section'></i>
        </div>                                        
    </div>
</template>

<script>

import { Todo } from '../js/Todo'

export default {

    props: [
        'todo',
    ],

    data: function () {
        return {
            todos: this.todo.todos
        }
    },

    methods: {
        addTodo: function(e) {
            this.todos.push(new Todo('New todo')) 
        }
    },
}
</script>

I can use the addTodo in my root component, but when trying to add new sub-todos I get

Cannot read property '_wrapper' of undefined

I tried modifying the data properties in other ways to make sure Vue picked up changes but had o success so far. Any ideas what error I might have here and how to fix it?

1 Answer 1

1

Maybe you were missing the name property on the component? I seem to be able to have it work once I added it (apart from the missing css).

https://codesandbox.io/s/dazzling-paper-dil66?file=/src/components/TodoCard.vue:695-713

e.g.

export default {
    name: "todo-card"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I have added the name property with same result. I see though you are registering the components globally which might help. Not sure if I can do that however since Im locked in a framework/file structure.
Maybe you need to expand your code to show how everything links together

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.