1、在默認(rèn)的情況下,Vue.js默認(rèn)不支持往數(shù)組中加入重復(fù)的數(shù)據(jù)。可以使用track-by="$index"來實現(xiàn)。
清鎮(zhèn)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
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)報錯
Vue.js默認(rèn)不支持往數(shù)組中加入重復(fù)的數(shù)據(jù)。可以使用track-by="$index"來實現(xiàn)。
總結(jié)
以上所述是小編給大家介紹的Vue.js在數(shù)組中插入重復(fù)數(shù)據(jù)的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
網(wǎng)站欄目:Vue.js在數(shù)組中插入重復(fù)數(shù)據(jù)的實現(xiàn)代碼
當(dāng)前網(wǎng)址:http://chinadenli.net/article4/gdjdoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、域名注冊、面包屑導(dǎo)航、手機網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)