vue 實(shí)現(xiàn)在同一界面實(shí)現(xiàn)組件的動態(tài)添加和刪除功能
1.插入靜態(tài)組件,將自己想要循環(huán)的組件顯示在頁面上,利用v-for進(jìn)行循環(huán)操作,操作包括增刪。
//所有要顯示的子組件,寫在一個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-select size="small" v-model="chooseValue" multiple placeholdr="請選擇試題類型" 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-select style="margin-left:-30px" size="small" v-model="item.value" placeholder="請選擇"> <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 class="el-icon-remove-outline" style="margin-left:20px" @click="questionList.splice(index, 1)"> </i>
4.效果
PS:在Vue組件上動態(tài)添加和刪除屬性方法
如下所示:
在組件上添加屬性 this.$set(this.data,"obj",value');
刪除屬性this.$delete(this.data,"obj",value');
總結(jié)
到此這篇關(guān)于vue 實(shí)現(xiàn)在同一界面實(shí)現(xiàn)組件的動態(tài)添加和刪除的文章就介紹到這了,更多相關(guān)vue 組件動態(tài)添加和刪除內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解在Vue中使用TypeScript的一些思考(實(shí)踐)
這篇文章主要介紹了詳解在Vue中使用TypeScript的一些思考(實(shí)踐),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Vue實(shí)現(xiàn)多點(diǎn)涂鴉效果,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03Vue Cli3 打包配置并自動忽略console.log語句的方法
這篇文章主要介紹了Vue Cli3 打包配置并自動忽略console.log語句的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Vue3+Element+Ts實(shí)現(xiàn)表單的基礎(chǔ)搜索重置等功能
本文主要介紹了Vue3+Element+Ts實(shí)現(xiàn)表單的基礎(chǔ)搜索重置等功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12在vue中使用echarts(折線圖的demo,markline用法)
這篇文章主要介紹了在vue中使用echarts(折線圖的demo,markline用法),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07