简体   繁体   English

方法调用后,输入中的值不会更新

[英]Value inside input doesn't get updatet after method call

I would like to create a todo-List app. 我想创建一个待办事项列表应用程序。 Here is my Code: HTML: 这是我的代码:HTML:

<div class="divPadder">
  <input ref="makePlaceholderEmpty" class="inputBoxSize" type="text" :placeholder="placeholder"v-model="task">
  <ul>
    <li v-for="(item,index) in this.tasks" :key="index">{{item}}</li>
  </ul>
</div>
<button class="button" v-on:click="pushAndMakePCEmpty">Submit</button>

script: 脚本:

data() {
return {
  placeholder:"put your notes here :)))",
  task: "",
  tasks: []
};


},
  methods: {
    pushAndMakePCEmpty() {
      this.$refs.makePlaceholderEmpty.value = "";
      this.tasks.push(this.task);
    }
  }

My problem is as soon as I add the v-for part into my html code that my value doesn't get updatet to a emtpy string as it should. 我的问题是,一旦将v-for部分添加到html代码中,我的值就不会像应有的那样被更新为emtpy字符串。 If I comment the v-for part out, the value attribute gets updatet. 如果我将v-for零件注释掉,则value属性将更新。 Hope somebody sees problem here. 希望有人在这里看到问题。

I've made a quick snippet using what you've already made, I believe this fits your expectations. 我已经使用您已经制作的内容做了一个简短的摘要,我相信这符合您的期望。

Just try and run the snippet ;D 只需尝试运行片段; D

 new Vue({ el: '#app', data() { return { placeholder:"put your notes here :)))", task: "", tasks: [] } }, methods: { pushAndMakePCEmpty() { this.tasks.push(this.task); this.task = ""; } } }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div class="divPadder"> <input ref="makePlaceholderEmpty" class="inputBoxSize" type="text" :placeholder="placeholder"v-model="task"> <ul> <li v-for="(item,index) in tasks" :key="index">{{item}}</li> </ul> </div> <button class="button" v-on:click="pushAndMakePCEmpty">Submit</button> </div> 

The code itself should be self-explanatory, but to point out the changes I've made: 代码本身应该是不言自明的,但是要指出我所做的更改:

  1. Removed the this from the v-for 从v-for中删除了这个
  2. Changed the way to reset the value from the input by using Vue's v-model interface through changing task data 通过更改任务数据,使用Vue的v-model界面,更改了从输入重置值的方式

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

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