代码之家  ›  专栏  ›  技术社区  ›  Suresh Subedi

为什么vue不显示列表中对象的内容和状态?

  •  0
  • Suresh Subedi  · 技术社区  · 5 年前

    我正在尝试在vue中创建crud应用程序。目前,它似乎可以创建、编辑和删除对象。但是它没有显示内容和反映添加/现有对象的状态。多次编辑条目也很奇怪。你能帮我理解为什么吗?如何正确显示内容?

    Vue.component('todo-item', {
      template: '\
                  <li>\
                    <input type="checkbox" v-model="isChecked" />\
                    <button v-on:click="$emit(\'remove\')">Delete</button>\
                    <button v-on:click="$emit(\'edit\')">Edit</button>\
                    {{ content }}\
                  </li>\
                ',
      props: ['content', 'isChecked']
    })
    var app = new Vue({
      el: '#app',
      data: {
        "todo": {
          isChecked: true
        },
        "todos": [{
            id: 1,
            isChecked: true,
            content: "hahaha"
          },
          {
            id: 2,
            isChecked: false,
            content: "hohoho"
          }
        ],
        "counter": 3
      },
      methods: {
        addNewTodo: function() {
          if (this.todo.id != undefined) {
            this.todos.filter(t => t.id === this.todo.id).content = this.todo.content;
            this.todos.filter(t => t.id === this.todo.id).content = this.todo.isChecked;
          } else {
            this.todos.push({
              id: this.counter++,
              content: this.todo.content,
              isChecked: this.todo.isChecked
            })
          }
          this.clear();
        },
        edit: function(todo) {
          this.todo = todo;
          console.log(todo);
        },
        clear: function() {
          this.todo.content = '';
          this.todo.isChecked = true;
          this.todo.id = undefined;
        }
      }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
      <form v-on:submit.prevent="addNewTodo">
        <input type="checkbox" id="checkbox" v-model="todo.isChecked" />
        <label for="checkbox">Done</label>
        <input v-model="todo.content" placeholder="edit me" />
        <button type="button" @click="clear">Clear</button>
      </form>
      <ul>
        <li is="todo-item" v-for="(todo, index) in todos" v-bind:key="todo.id" v-bind:title="todo.content" v-on:remove="todos.splice(index, 1)" v-on:edit="edit(todo)"></li>
      </ul>
    </div>
    0 回复  |  直到 5 年前