简体   繁体   English

Vue:无法将值从父组件传递给子组件

[英]Vue : Cannot pass the value from parent to child component

Hi I was learning vue and I am trying to make a todo app, I cannot show the todo list items in my todo app, I can't figure out what is the problem is.嗨,我正在学习 vue,我正在尝试制作一个待办事项应用程序,我无法在我的待办事项应用程序中显示待办事项列表项,我无法弄清楚问题是什么。 This is my App.vue :这是我的App.vue

<template>
  <TodoList id="app" v-bind:todos="todos" />
</template>

<script>
import TodoList from './components/list.vue';

export default {
  name: 'App',
  data() {
    return {
      todos: [
        {
          id: 1,
          title: 'Go workout',
          completed: false
        },
        {
          id: 2,
          title: 'Do laundry',
          completed: false
        },
        {
          id: 3,
          title: 'Cook food',
          completed: false
        }
      ]
    };
  },
  components: {
    TodoList
  }
};
</script>

This is my list.vue :这是我的list.vue

<template>
  <div>
    <h2>Todo List</h2>
    <ul>
      <li v-bind:key="todo.id" v-for="todo in todos">
        <Todo v-bind:todo="todo" />
      </li>
    </ul>
  </div>
</template>

<script>
import Todo from './todo.vue';

export default {
  name: 'TodoList',
  components: {
    Todo
  },
  created() {
    console.log(this);
  }
};
</script>

This component contains the Todo item, this is todo.js :该组件包含 Todo 项,这是todo.js

<template>
  <div>{{ todo.title }}</div>
</template>

<script>
export default {
  name: 'Todo',
  props: ['todo'],
  created() {
    console.log('todo', this);
  }
};
</script>

But todo is probably not being created, as the created lifecycle is not being called here.但是 todo 可能没有被创建,因为这里没有调用created的生命周期。 Help me figure what is the problem is.帮我弄清楚问题是什么。

You forgot the props attribute in your TodoList component:您忘记了 TodoList 组件中的 props 属性:

export default {
  name: 'TodoList',
  props: ['todos'],
  components: {
    Todo
  },
  created() {
    console.log(this);
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM