vue使用splice()刪除數(shù)組中的一個(gè)數(shù)據(jù) 彈出窗口提示問(wèn)題
使用splice()刪除數(shù)組中的一個(gè)數(shù)據(jù) 彈出窗口提示
1. 循環(huán)輸出數(shù)組時(shí) v-for="(item,index) in list" //需要獲取當(dāng)前下標(biāo),item
2. 點(diǎn)擊刪除按鈕傳一個(gè)參數(shù) , 在這個(gè)彈窗口方法中獲取到 這個(gè)參數(shù)。 在data中設(shè)置一個(gè)空置,把參數(shù)賦給這個(gè)值。
3. 彈窗中點(diǎn)擊刪除按鈕 這個(gè)數(shù)組.splice(獲取到的下標(biāo)值,1);
vue中splice()方法實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行增刪改操作
語(yǔ)法結(jié)構(gòu):splice(index,len,[item])
1、可以用來(lái)添加/刪除/替換數(shù)組內(nèi)某一個(gè)或者幾個(gè)值
2、該方法會(huì)改變?cè)紨?shù)組
index:數(shù)組開始下標(biāo)len: 替換/刪除的長(zhǎng)度item:替換的值,刪除操作的話 item為空
刪除
index表示要?jiǎng)h除的數(shù)組下標(biāo), len長(zhǎng)度為1(len設(shè)置1,如果為0,則數(shù)組不變),item為空表示執(zhí)行刪除操作
<div id="demo">
? <h2>v-for 遍歷數(shù)組</h2>
? <ul>
? ? <li v-for="(item, index) in persons" :key="index">
?? ? ? ? ?序號(hào):{{index}}
?? ? ? ? ?名字:{{item.name}}
?? ? ? ? ?年齡:{{item.age}}
? ? ?<button @click="del(index)">刪除</button>
? ? </li>
? </ul>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
? new Vue({
? ? el: '#demo',
? ? data: {
? ? ? persons: [
? ? ? ? {name: 'Tom', age:18},
? ? ? ? {name: 'Jack', age:17},
? ? ? ? {name: 'Bob', age:19},
? ? ? ? {name: 'Mary', age:16}
? ? ? ]
? ? },
? ? methods: {
? ? ? //刪除(注意:刪除的方法名不能用delete,因?yàn)閐elete是系統(tǒng)關(guān)鍵字)
? ? ? del(index) {
? ? ? ? this.persons.splice(index, 1)?
? ? ? }
? ? }
? })
</script>替換(修改)
相當(dāng)于是先刪除,再添加
<div id="demo">
? <h2>測(cè)試: v-for 遍歷數(shù)組</h2>
? <ul>
? ? <li v-for="(item, index) in persons" :key="index">
?? ? ? ? ?序號(hào):{{index}}
?? ? ? ? ?名字:{{item.name}}
?? ? ? ? ?年齡:{{item.age}}
? ? ?<button @click="update(index, {name:'張三', age: 16})">更新</button>
? ? </li>
? </ul>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
? new Vue({
? ? el: '#demo',
? ? data: {
? ? ? persons: [
? ? ? ? {name: 'Tom', age:18},
? ? ? ? {name: 'Jack', age:17},
? ? ? ? {name: 'Bob', age:19},
? ? ? ? {name: 'Mary', age:16}
? ? ? ]
? ? },
? ? methods: {
? ? ? //修改
? ? ? update(index, item) {
? ? ? ? this.persons.splice(index, 1, item)
? ? ? }
? ? }
? })
</script>添加
index下標(biāo)直接設(shè)置為0,len長(zhǎng)度也設(shè)置為0,item傳入要添加的對(duì)象
<div id="demo">
? <h2>測(cè)試: v-for 遍歷數(shù)組</h2>
? <ul>
? ? <li v-for="(item, index) in persons" :key="index">
?? ? ? ? ?序號(hào):{{index}}
?? ? ? ? ?名字:{{item.name}}
?? ? ? ? ?年齡:{{item.age}}
? ? </li>
? </ul>
? <button @click="add({name: '李四', age: 18})">添加</button>
</div>
<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript">
? new Vue({
? ? el: '#demo',
? ? data: {
? ? ? persons: [
? ? ? ? {name: 'Tom', age:18},
? ? ? ? {name: 'Jack', age:17},
? ? ? ? {name: 'Bob', age:19},
? ? ? ? {name: 'Mary', age:16}
? ? ? ]
? ? },
? ? methods: {
? ? ? ? //添加
? ? ? ? add (item) {
? ? ? ? ? ? this.persons.splice(0, 0, item)
? ? ? ? }
? ? }
? })
</script>附加知識(shí)點(diǎn)
在v-for遍歷中,都需要聲明:key,那么:key的作用是什么呢?
答:vue和react都實(shí)現(xiàn)了一套虛擬DOM,使我們可以不直接操作DOM元素,只操作數(shù)據(jù)便可以重新渲染頁(yè)面。而隱藏在背后的原理便是其高效的Diff算法。vue和react的虛擬DOM的Diff算法大致相同,其核心是基于兩個(gè)簡(jiǎn)單的假設(shè):
- 假設(shè)1、 兩個(gè)相同的組件產(chǎn)生類似的DOM結(jié)構(gòu),不同的組件產(chǎn)生不同的DOM結(jié)構(gòu)。
- 假設(shè)2、 同一層級(jí)的一組節(jié)點(diǎn),他們可以通過(guò)唯一的id進(jìn)行區(qū)分。
簡(jiǎn)單的說(shuō), :key的作用主要是為了高效的更新虛擬DOM
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
前端本地存儲(chǔ)方案localForage在vue3中使用方法
localForage是前端本地存儲(chǔ)的庫(kù),支持多種存儲(chǔ)后端,并提供了一致的API來(lái)存儲(chǔ)和檢索數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于前端本地存儲(chǔ)方案localForage在vue3中使用的相關(guān)資料,需要的朋友可以參考下2024-09-09
vue+iview的菜單與頁(yè)簽的聯(lián)動(dòng)方式
這篇文章主要介紹了vue+iview的菜單與頁(yè)簽的聯(lián)動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開時(shí)縮小的代碼
這篇文章主要介紹了vue.js實(shí)現(xiàn)點(diǎn)擊圖標(biāo)放大離開時(shí)縮小,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
解決修復(fù)報(bào)錯(cuò)Error in render:TypeError:Cannot read&n
這篇文章主要介紹了解決修復(fù)報(bào)錯(cuò)Error in render:TypeError:Cannot read properties of undefined(reading ‘ipconfig‘)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
解決Vue項(xiàng)目打包后打開index.html頁(yè)面顯示空白以及圖片路徑錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決Vue項(xiàng)目打包后打開index.html頁(yè)面顯示空白以及圖片路徑錯(cuò)誤的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

