使用组件或v模型使用Vue js 2进行列表

时间:2018-03-06 13:34:46

标签: vuejs2

您好我在这里有一个代码,在Vuejs中有两个“待办事项列表”实现,但我有一个问题。

1使用vue组件我对如何使用父变量有所了解。

2在main函数上执行此操作我无法保留discard实现的旧值。

请找到工作代码

运行! todo list in codepen

Vue.component('ntodo-item', {

  template: '\
   <transition name="fade">\
      <div id="if" class="row" v-if="edit">\
        <div class="col-md-7">\
          <input class="form-control"  v-model="title">\
        </div>\
        <div id="sssss" class="col-md-5">\
          <button class="btn btn-danger roundButton" v-on:click="$emit(\'edit\')">Discard</button>\
          <button class="btn btn-success roundButton" v-on:click="updateValue">Save</i></button>\
        </div>\
      </div>\
      <div id="else" class="row" v-else>\
        <div class="col-md-7">\
          {{ title }}\
        </div>\
        <div id="ssaaas" class="col-md-5">\
          <button class="btn btn-danger roundButton" v-on:click="$emit(\'remove\')">Remove</button>\
          <button id="aaa" class="btn btn-default roundButton" v-on:click="$emit(\'edit\')">Edit</button>\
        </div>\
      </div>\
    </transition>\
  ',

  props: [
    'title' ,
    'edit'
],
  methods: {
    updateValue: function () {
      this.$emit('input', this.title);
    }
  }
})


var app14 = new Vue({
  el: '#app-14',
  data: {
    newTodoText: '',
    newTodoText2: '',
    todos: [
      {
        id: 1,
        title: 'Do the dishes',
        edit:0
      },
      {
        id: 2,
        title: 'Take out the trash',
        edit:0
      },
      {
        id: 3,
        title: 'Mow the lawn',
        edit:0
      }
    ],
    todos2: [
      {
        id: 1,
        title: 'Do the dishes',
        edit:0
      },
      {
        id: 2,
        title: 'Take out the trash',
        edit:0
      },
      {
        id: 3,
        title: 'Mow the lawn',
        edit:0
      }
    ],
    nextTodoId: 4,
    nextTodoId2: 4
  },
  methods: {

    addNewTodo: function () {
      this.todos.push({
        id: this.nextTodoId++,
        title: this.newTodoText,
        edit:0
      })
      this.newTodoText = ''
      this.todos = _.orderBy(this.todos, 'id', 'desc');
    },

    editTodo: function (item){
      // console.log(item.title)
      item.edit^= 1
    },
    updateValue: function (item, newValue){
      item.title=newValue
      item.edit^= 1
    },


    addNewTodo2: function () {
      this.todos2.push({
        id: this.nextTodoId2++,
        title: this.newTodoText2,
        edit:0
      })
      this.newTodoText2 = ''
      this.todos2 = _.orderBy(this.todos2, 'id', 'desc');
    },
    editTodo2: function (item){
       console.log(item.title)
      item.edit^= 1
    },
    deleteTodo2: function (item){
      this.todos2.splice(item.id, 1);
    },
    updateValue2: function(text){
      console.log(text);
    }



  }
})
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s, transform 0.3s;
  transform-origin: left center;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
 transform: scale(0.5);
}
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>
 <div class="col-md-12">
        <div class="graybox">
          <h5>app14</h5>
          <div id="app-14">
			`enter code here`<div class="row">
            	<div class="col-md-6">
            <h5> todo list using "ntodo-item" component</h5>
														<p>This one show me a warning because the child cannot edit the va passed by the parent but it is working and spected</p>
              <input class="form-control"
                v-model="newTodoText"
                v-on:keyup.enter="addNewTodo"
                placeholder="Add a todo"
              >
              <hr>
              <ul>
                <li
                  is="ntodo-item"
                  v-for="(todo, index) in todos"
                  v-bind:key="todo.id"
                  v-bind:title="todo.title"
                  v-bind:edit="todo.edit"
                  v-on:input="updateValue(todo, $event)"
                  v-on:remove="todos.splice(index, 1)"
                  v-on:edit="editTodo(todo)"
                ></li>
              </ul>
          			</div>
											
          <div class="col-md-6">
          <h5> todo list update</h5>
												<p> This one is working without any warn but I dont know how to discard changes. I dont want to create a temp var because I want to be able to edit all of them at the same time. </p>
            <input v-model="newTodoText2"
              v-on:keyup.enter="addNewTodo2"
              placeholder="Add a todo"
              class="form-control"
            >
            <hr>
            <ul>
              <transition-group name="fade" >
                <li v-for="(todo2, index) in todos2":key="todo2.id">
                  <div id="if" class="row" v-if="todo2.edit">
                   <div class="col-md-7">
                     <input class="form-control" ref="todo2" v-model="todo2.title">
                   </div>
                   <div id="sssss" class="col-md-5">
                     <button class="btn btn-success roundButton" v-on:click="editTodo2(todo2)">ok</button>
                   </div>
                  </div>
                  <div id="else" class="row" v-else>
                   <div class="col-md-7">
                     {{todo2.title}}
                   </div>
                   <div id="ssaaas" class="col-md-5">
                     <button class="btn btn-danger roundButton" v-on:click="todos2.splice(index, 1)">Remove</button>
                     <button id="aaa" class="btn btn-default roundButton" v-on:click="editTodo2(todo2)">Edit</button>
                   </div>
                  </div>
                </li>
              </transition>

            </ul>
        </div>
													</div>	
      </div>
    </div>
  </div>

  .

1 个答案:

答案 0 :(得分:0)

回应我的评论:

创建title道具的局部变量副本,并在编辑时发出该变量的更改。如果他们放弃编辑,只需将局部变量重置为title道具的值。关于CodeSandbox here的工作示例。

Todo项目组件

<button class="btn btn-danger roundButton" @click="discardEdit">Discard</button>

...

 data() {
    return {
      // our local copy
      localTitle: null,
    };
  },
  mounted() {
    this.localTitle = this.title;
  },
  methods: {
    updateValue: function() {
      this.$emit("input", this.localTitle);
    },
    discardEdit: function() {
      // just set local back to title prop value
      this.localTitle = this.title;
      this.$emit('edit');
    },
  }