Vue實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法
更新時(shí)間:2018年03月17日 12:38:35 作者:世英
下面小編就為大家分享一篇Vue實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
視圖:

代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
//導(dǎo)入vue.js
<script type="text/javascript" src="./vue.js"></script>
//非常簡(jiǎn)單了設(shè)置了一下css樣式
<style type="text/css">
#app{
height: 100%;
margin-left: 200px;
width:50%;
text-align: center;
background-color: lightpink
}
.tab{
width: 600px;
margin-top: 30px;
background-color: lightpink;
}
input{
height: 25px;
margin-top: 10px;
border-radius:5px;
}
</style>
</head>
<body>
<div id="app">
<div class="createForm">
姓名:<input type="text" name="uname" v-model="userName"><br>
年齡:<input type="text" name="uage" id="uage" v-model="userAge"><br>
性別:<select name="gender" v-model="selected">
<option v-for="option in options" v-bind:value="option.gender">
{{option.gender}}
</option>
</select>
{{selected}}
<br>
<button type="button" v-on:click="insertInfo">創(chuàng)建</button>
</div>
<table class="tab">
<tr style="background-color: pink">
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>刪除</th>
</tr>
<tr v-for="(person,index) in infoArr">
<td>{{person.uname}}</td>
<td>{{person.uage}}</td>
<td>{{person.gender}}</td>
<td><button v-on:click="deleteInfo(index)">刪除</button></td>
</tr>
</table>
</div>
</body>
</html>
<script type="text/javascript">
new Vue({
el:"#app",
data:{
msg:"hello",
selected:'女',
userName:'',
userAge:'',
options:[
{gender:"男"},
{gender:"女"}
],
infoArr:[]
},
methods:{
//添加數(shù)據(jù)的方法
insertInfo(){
var obj={};
obj.uname=this.userName;
obj.uage=this.userAge;
obj.gender=this.selected;
this.infoArr.push(obj);
console.log(obj);
},
//刪除的方法
deleteInfo(index){
this.infoArr.splice(index,1);
}
}
})
</script>
以上這篇Vue實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建和刪除數(shù)據(jù)的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js綁定事件監(jiān)聽(tīng)器示例【基于v-on事件綁定】
這篇文章主要介紹了vue.js綁定事件監(jiān)聽(tīng)器的方法,結(jié)合實(shí)例形式分析了vue.js基于v-on事件綁定響應(yīng)鼠標(biāo)點(diǎn)擊相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
vue實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能詳解
這篇文章主要介紹了vue實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能詳解的相關(guān)資料,需要的朋友可以參考下2022-09-09
Vue-cli集成axios請(qǐng)求出現(xiàn)CORS跨域問(wèn)題及解決
這篇文章主要介紹了Vue-cli集成axios請(qǐng)求出現(xiàn)CORS跨域問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-10-10
Vue利用watch偵聽(tīng)器模擬實(shí)現(xiàn)翻譯功能
本期將會(huì)介紹 Vue 中的 watch 偵聽(tīng)器,它語(yǔ)法是怎么樣的呢?具有怎樣的功能呢?最后用模擬實(shí)現(xiàn)百度翻譯來(lái)更進(jìn)一步練習(xí) watch 偵聽(tīng)器,需要的朋友可以參考下2024-11-11
Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問(wèn)題
這篇文章主要介紹了Vue項(xiàng)目添加前綴,ngnix發(fā)布相關(guān)修改問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

