简体   繁体   English

清空 Vue.js 数组

[英]Emptying Vue.js Array

I recently started using Vue.js, and have ran into a small problem.我最近开始使用 Vue.js,遇到了一个小问题。 I have an array, and have used Vue.js to add that array into the rendered code.我有一个数组,并使用 Vue.js 将该数组添加到呈现的代码中。 Using standard .push works fine for that.使用标准.push可以很好地解决这个问题。

However, I also want to be able to clear the array which would clear the rendered code.但是,我也希望能够清除将清除渲染代码的数组。 But when I try array = [] to clear it, the code doesn't work, and everything stops working.但是当我尝试array = []清除它时,代码不起作用,一切都停止工作。

How do I clear the list without breaking the program?如何在不破坏程序的情况下清除列表?

I replicated the problem in the below snippet.我在下面的代码片段中复制了这个问题。

 let results = [1, 2]; let num = 3; var app = new Vue({ el: '#app', data: { results: results } }); document.getElementById("add").addEventListener("click", function() { results.push(num); num++; }); document.getElementById("clear").addEventListener("click", function() { results = []; num = 1; });
 .as-console-wrapper { height: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <button id="add">Add</button> <button id="clear">Clear</button> <div id="app"> <h1 v-for="result in results"> {{ result }}</h1> </div>

So there are a couple of small things that I updated in your app to be done the vue way , as opposed to the old vanilla JS approach.所以我在你的应用程序中更新了一些小事情,以vue 的方式完成,而不是旧的 vanilla JS 方法。

  • First off we manage all component data within the data section, so you don't need your above JS variables.首先,我们管理data部分中的所有组件数据,因此您不需要上面的 JS 变量。
  • It's also a good approach to return a function that returns your json here, rather than just have your json object raw.返回一个在此处返回您的 json 的函数也是一种好方法,而不仅仅是让您的 json 对象原始。
  • Next, you no longer need to use that old-school document.getElementById("add").addEventListener approach, instead just return your functions in the methods , and then simply call to them with the v-on:click="addNew" attribute接下来,您不再需要使用老式的document.getElementById("add").addEventListener方法,而只需在methods中返回您的函数,然后只需使用v-on:click="addNew"调用它们属性

 new Vue({ el: "#app", data: () => { return { results: [3, 1, 4, 1, 5] }; }, methods: { addNew() { const nextNum = this.$data.results.length + 1; this.$data.results.push(nextNum); }, clearAll(){ this.$data.results = []; } } })
 .as-console-wrapper { height: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <h2>Awesome Counting App</h2> <button v-on:click="addNew">Add</button> <button v-on:click="clearAll">Clear</button> <hr> <ul> <li v-for="result in results" v-bind:key="result"> {{result}} </li> </ul> </div>

Solution 1:解决方案1:

Move the buttons inside your #app and use Vue's v-on:click handler.移动#app 中的按钮并使用 Vue 的v-on:click处理程序。

 let results = [1, 2]; let num = 3; var app = new Vue({ el: '#app', data: { results: results }, methods: { add: function() { this.results.push(num); num++; }, clear: function() { this.results = []; num = 1; } } });
 .as-console-wrapper { height: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <button v-on:click="add">Add</button> <button v-on:click="clear">Clear</button> <h1 v-for="result in results"> {{ result }}</h1> </div>


Solution2: Use Vue's $data API解决方案2:使用 Vue 的$data API

 let results = [1, 2]; let num = 3; var app = new Vue({ el: '#app', data: { results: results } }); document.getElementById("add").addEventListener("click", function() { app.$data.results.push(num) num++; }); document.getElementById("clear").addEventListener("click", function() { app.$data.results = []; num = 1; });
 .as-console-wrapper { height: 0px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <button id="add">Add</button> <button id="clear">Clear</button> <div id="app"> <h1 v-for="result in results"> {{ result }}</h1> </div>

Make it more Vue ish!让它更 Vue ish!

<button @click="add">Add</button>
<button @click="clear">Clear</button>

var app = new Vue({
      el: '#app',
      data: {
        results: [1, 2],
        num : 3
      }, 
      methods(){
          add(){
            this.results.push(num);
            this.num++;
          },
          clear(){
           this.results = [];
           this.num = 1;
          }
      }

    });

If you working with Vuejs you don't need of declare any variable out Vue instance, and any vanilla code outside them.如果你使用 Vuejs,你不需要在 Vue 实例之外声明任何变量,以及它们之外的任何 vanilla 代码。

You can use vanilla inside vue instance, but for now, you dont need it:你可以在 vue 实例中使用 vanilla,但现在你不需要它:

document.getElementById("add").addEventListener("click", function() {
  results.push(num);
  num++;
});

document.getElementById("clear").addEventListener("click", function() {
  results = [];
  num = 1;
});

Instead you may use like this:相反,您可以这样使用:

new Vue({
el: '#app',
data: {
    results: [],
    num: 1
},
methods: {
    addNumberArray(){
    this.results.push(number);
    number++;
  },
  clearNumberArray(){
    this.results = [];
    number = 1;
  }
}
});

Here a live example: https://jsfiddle.net/n4krde0h/19/这是一个活生生的例子: https ://jsfiddle.net/n4krde0h/19/

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

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