Vue.js在數(shù)組中插入重復(fù)數(shù)據(jù)的實(shí)現(xiàn)代碼
1、在默認(rèn)的情況下,Vue.js默認(rèn)不支持往數(shù)組中加入重復(fù)的數(shù)據(jù)??梢允褂?code>track-by="$index"來實(shí)現(xiàn)。
2、不使用track-by="$index"的數(shù)組插入,數(shù)組不支持重復(fù)數(shù)據(jù)的插入
2.1 JavaScript代碼
<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代碼
<div id="app"> <!--顯示數(shù)據(jù)--> <ul> <li v-for="value in arrMsg" > {{value}} </li> </ul> <button type="button" @click="add">增加數(shù)據(jù)</button> </div>
2.2 結(jié)果
3、使用track-by="$index"的數(shù)組插入,數(shù)組支持重復(fù)數(shù)據(jù)的插入
3.1 Javascript代碼
<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代碼
<div id="app" class="container"> <!--顯示數(shù)據(jù)--> <ul> <li v-for="value in arrMsg" track-by="$index" > {{value}} </li> </ul> <button type="button" @click="add" >增加數(shù)據(jù)</button> </div>
3.3 結(jié)果
4、完整代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="../css/bootstrap.min.css" rel="external nofollow" /> <style type="text/css"> .container{ margin-top: 20px; } </style> <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> </head> <body> <div id="app" class="container"> <!--顯示數(shù)據(jù)--> <ul> <li v-for="value in arrMsg" track-by="$index" > {{value}} </li> </ul> <button type="button" @click="add" >增加數(shù)據(jù)</button> </div> </body> </html>
ps:下面看下vue 數(shù)組重復(fù),循環(huán)報(bào)錯(cuò)
Vue.js默認(rèn)不支持往數(shù)組中加入重復(fù)的數(shù)據(jù)??梢允褂?code>track-by="$index"來實(shí)現(xiàn)。
總結(jié)
以上所述是小編給大家介紹的Vue.js在數(shù)組中插入重復(fù)數(shù)據(jù)的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue3使用vueup/vue-quill富文本、并限制輸入字?jǐn)?shù)的方法處理
這篇文章主要介紹了vue3使用vueup/vue-quill富文本、并限制輸入字?jǐn)?shù),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03關(guān)于vue中標(biāo)簽的屬性綁定值渲染問題
這篇文章主要介紹了關(guān)于vue中標(biāo)簽的屬性綁定值渲染問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06vue3+springboot部署到Windows服務(wù)器的詳細(xì)步驟
這篇文章主要介紹了vue3+springboot部署到Windows服務(wù)器,配置Nginx時(shí),因?yàn)楝F(xiàn)在是把vue前端交給了Nginx代理,所以這里的端口號不一定是我們在vue項(xiàng)目中設(shè)置的端口號,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-10-10在Vue中使用scoped屬性實(shí)現(xiàn)樣式隔離的原因解析
scoped是Vue的一個(gè)特殊屬性,可以應(yīng)用于<style>標(biāo)簽中的樣式,這篇文章給大家介紹在Vue中,使用scoped屬性為什么可以實(shí)現(xiàn)樣式隔離,感興趣的朋友一起看看吧2023-12-12