vue 實(shí)現(xiàn)在同一界面實(shí)現(xiàn)組件的動(dòng)態(tài)添加和刪除功能
1.插入靜態(tài)組件,將自己想要循環(huán)的組件顯示在頁(yè)面上,利用v-for進(jìn)行循環(huán)操作,操作包括增刪。
//所有要顯示的子組件,寫(xiě)在一個(gè)li標(biāo)簽,有 v-for循環(huán)。 <li v-for='(item, index) in questionList' v-bind:key='index'> <el-row :gutter='20'> <el-col offset='2' :span='4'> <el-selectsize='small'v-model='chooseValue'multipleplaceholdr='請(qǐng)選擇試題類(lèi)型'style='width:200px;margin-left:-40px' ><el-option :value='item.mineStatusValue' style='height:auto'> <el-tree :data='data' node-key='id' :props='defaultProps' @node-click='handleNodeClick' ></el-tree></el-option> </el-select> </el-col> <el-col offset='3' :span='3'> <el-selectstyle='margin-left:-30px'size='small'v-model='item.value'placeholder='請(qǐng)選擇'><el-option v-for='item in questionoptions' :key='item.value' :label='item.label' :value='item.value'></el-option> </el-select> </el-col> <el-col offset='2' :span='4'> <el-input style='width:45px;' size='small' v-model='count'>{{ item.count }} </el-input> <label> /0 </label> </el-col> <el-col :span='6'> <el-input style='width:45px;' size='small' v-model='sourcess'>{{ item.sourcess }} </el-input> <label> </label> <!-- 在i標(biāo)簽設(shè)置刪除按鈕,運(yùn)用splice函數(shù)。 --> <i class='el-icon-remove-outline'style='margin-left:20px'@click='questionList.splice(index, 1)' ></i>
2.增
在方法添加按鈕,在點(diǎn)擊添加按鈕的時(shí)候?qū)⑿枰膮?shù)傳進(jìn)數(shù)組,遍歷數(shù)組,達(dá)到組件渲染。
//添加試題,組件循環(huán)顯示 addQuestion() { console.log('添加試題'); this.questionList.unshift({mineStatusValue: this.mineStatusValue,questionoptions: this.questionoptions,count: this.count,sourcess: this.sourcess, }); },
3.刪
根據(jù)選中的組件,獲取它的索引,根據(jù)下標(biāo)把它刪掉,更新數(shù)組,重選渲染組件。
注:刪除的時(shí)候用到了splic函數(shù),具體的使用可以自己了解。
<i @click='questionList.splice(index, 1)'></i>
4.效果

PS:在Vue組件上動(dòng)態(tài)添加和刪除屬性方法
如下所示:
在組件上添加屬性 this.$set(this.data,'obj',value’);
刪除屬性this.$delete(this.data,'obj',value’);
總結(jié)
到此這篇關(guān)于vue 實(shí)現(xiàn)在同一界面實(shí)現(xiàn)組件的動(dòng)態(tài)添加和刪除的文章就介紹到這了,更多相關(guān)vue 組件動(dòng)態(tài)添加和刪除內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP使用Swagger生成好看的API文檔2. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條3. Python3 json模塊之編碼解碼方法講解4. Python 制作查詢商品歷史價(jià)格的小工具5. Python 如何調(diào)試程序崩潰錯(cuò)誤6. Python 利用Entrez庫(kù)篩選下載PubMed文獻(xiàn)摘要的示例7. ASP基礎(chǔ)知識(shí)VBScript基本元素講解8. python使用jenkins發(fā)送企業(yè)微信通知的實(shí)現(xiàn)9. Python sublime安裝及配置過(guò)程詳解10. Python 合并拼接字符串的方法

網(wǎng)公網(wǎng)安備