Home > Article > Web Front-end > Detailed explanation of Vue.js implementation code for inserting repeated data into arrays
This article mainly introduces the implementation code of Vue.js inserting repeated data into an array. Friends who need it can refer to it. I hope it can help everyone.
1. By default, Vue.js does not support adding repeated data to an array. This can be achieved using track-by="$index"
.
2. Do not use track-by="$index" for array insertion. The array does not support the insertion of duplicate data.
2.1 JavaScript code
<script type="text/javascript" src="../js/vue-1.0.21.js"></script> <script type="text/javascript"> window.onload = function() { vm = new Vue({ el: '#app', data: { arrMsg: ['apple', 'orage', 'pear'] }, methods: { add: function() { this.arrMsg.push('tamota'); } } }); } </script>
2.2 html code
<p id="app"> <!--显示数据--> <ul> <li v-for="value in arrMsg" > {{value}} </li> </ul> <button type="button" @click="add">增加数据</button> </p>
2.2 Result
3. Use track-by= Array insertion of "$index", the array supports the insertion of repeated data
3.1 Javascript code
<script type="text/javascript" src="../js/vue-1.0.21.js"></script> <script type="text/javascript"> window.onload = function() { vm = new Vue({ el: '#app', data: { arrMsg: ['apple', 'orage', 'pear'] }, methods: { add: function() { this.arrMsg.push('tamota'); } } }); } </script>
3.2 html code
<p id="app" class="container"> <!--显示数据--> <ul> <li v-for="value in arrMsg" track-by="$index" > {{value}} </li> </ul> <button type="button" @click="add" >增加数据</button> </p>
3.3 Results
4. Complete code
<script type="text/javascript" src="../js/vue-1.0.21.js"></script> <script type="text/javascript"> window.onload = function() { vm = new Vue({ el: '#app', data: { arrMsg: ['apple', 'orage', 'pear'] }, methods: { add: function() { this.arrMsg.push('tamota'); } } }); } </script> <p id="app" class="container"> <!--显示数据--> <ul> <li v-for="value in arrMsg" track-by="$index" > {{value}} </li> </ul> <button type="button" @click="add" >增加数据</button> </p>
ps: Let’s take a look at the duplication of vue arrays and loop errors
Vue.js does not support adding repeated data to arrays by default. This can be achieved using track-by="$index"
.
Related recommendations:
The most detailed vue.js installation tutorial
Vue.js develops a globally called MessageBox Component
Detailed explanation of learning common instructions of Vue.js
The above is the detailed content of Detailed explanation of Vue.js implementation code for inserting repeated data into arrays. For more information, please follow other related articles on the PHP Chinese website!