<div style="padding-left: 190px" class="form-inline">
<!--<div v-for="(d,index) in counter" :key="index" style="padding-left: 190px" class="form-inline">-->
<!--<div v-for="(d,index) in counter" :key="index" style="padding-left: 190px" class="form-inline">-->
<!--此處生成黨員表單-->
<!--將外部組件放到vue范圍內-->
<member-info v-for="(item,index) in counter" :key="index" :content="item" :index="index" @delete="btn_delete"></member-info>
</div>
<div style="padding-left: 270px;padding-top: 20px">
<input v-on:click="btn_add" id="btn_add" class="btn btn-info" type="button" value="https://bbs.csdn.net/topics/添加一個黨員資訊">
</div>
<!--外部定義組件,外部定義的組件不能寫在vue范圍內-->
<template id="tmp">
<div><!--組件內有多個標簽必須有一個父div包起來-->
<div class="form-group">
<span style="color: red">*</span>姓名: <label class="sr-only" for="exampleInputEmail3"></label>
<input type="email" class="form-control" id="exampleInputEmail3">
</div>
<div class="form-group">
<span style="color: red">*</span>性別:<label class="sr-only" for="exampleInputPassword3"></label>
<input type="password" class="form-control">
</div>
<div class="form-group">
<span style="color: red">*</span>出生年月:<label class="sr-only" for="exampleInputPassword3"></label>
<input type="password" class="form-control">
</div>
<div class="form-group">
<span style="color: red">*</span>所屬黨組織:<label class="sr-only" for="exampleInputPassword3"></label>
<input type="password" class="form-control" id="exampleInputPassword3">
</div>
<input @click="btn_delete" id="btn_delete" type="button" class="btn btn-danger" value="https://bbs.csdn.net/topics/洗掉">
<br/><br/>
</div>
</template>
/*vue組件*/
Vue.component('member-info', {
template: '#tmp',
props:['content',"index"],
methods: {
btn_delete:function () {
alert(this.index);
this.$emit("delete",this.index)
},
}//methods END
});
var vm = new Vue({
el:'#vue_div',
data:{
counter:[],
},
methods:{
/*添加一個黨員資訊按鈕 被點擊,就會插入一個新組件*/
btn_add:function () {
this.counter.push({});
},
btn_delete:function (index) {
alert("洗掉");
this.counter.splice(index, 1)
},
}//methods END
});//new Vue END

以上的代碼是點擊id="btn_add"按鈕會生成一個組件。然后點擊id="btn_delete"按鈕會洗掉當前點擊的組件,但是總是會洗掉最后一個,index下標是正確的。我該如何更改?謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/281109.html
標籤:JavaScript
