下面將介紹兩種方式實(shí)現(xiàn)動(dòng)態(tài)添加和刪除列表

1.不使用組件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 測(cè)試實(shí)例 - vue實(shí)現(xiàn)列表增加和刪除</title>
<script src="https://cdn.bootcss.com/vue/2.2.2/vue.min.js"></script>
</head>
<body>
<div id="app">
<input v-model="newAddText" placeholder="請(qǐng)輸入要添加的內(nèi)容" />
<button @click='addNewList'>添加</button>
<ul>
<li v-for='(list,index) in lists' v-bind:key='list.id'>
{{list.title}} <button v-on:click='lists.splice(index, 1)'>刪除</button>
</li>
</ul>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
newAddText:'',
lists:[
{id:1,title:'手機(jī)號(hào)碼'},
{id:2,title:'qq號(hào)'},
{id:3,title:'姓名'},
],
nextTodoId: 4
},
methods:{
addNewList:function(){
this.lists.push({
id:this.nextTodoId++,
title:this.newAddText
})
this.newAddText=''
}
}
})
</script>
</body>
</html>
分享標(biāo)題:Vue實(shí)現(xiàn)列表動(dòng)態(tài)添加和刪除的兩種方法小結(jié)-創(chuàng)新互聯(lián)
鏈接地址:http://www.chinadenli.net/article22/idjjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、服務(wù)器托管、用戶(hù)體驗(yàn)、商城網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容