vue使用splice()刪除數(shù)組中的一個數(shù)據(jù) 彈出窗口提示問題
使用splice()刪除數(shù)組中的一個數(shù)據(jù) 彈出窗口提示
1. 循環(huán)輸出數(shù)組時 v-for="(item,index) in list" //需要獲取當前下標,item
2. 點擊刪除按鈕傳一個參數(shù) , 在這個彈窗口方法中獲取到 這個參數(shù)。 在data中設置一個空置,把參數(shù)賦給這個值。
3. 彈窗中點擊刪除按鈕 這個數(shù)組.splice(獲取到的下標值,1);
vue中splice()方法實現(xiàn)對數(shù)組進行增刪改操作
語法結構:splice(index,len,[item])
1、可以用來添加/刪除/替換數(shù)組內某一個或者幾個值
2、該方法會改變原始數(shù)組
index
:數(shù)組開始下標len
: 替換/刪除的長度item
:替換的值,刪除操作的話 item為空
刪除
index表示要刪除的數(shù)組下標, len長度為1(len設置1,如果為0,則數(shù)組不變),item為空表示執(zhí)行刪除操作
<div id="demo"> ? <h2>v-for 遍歷數(shù)組</h2> ? <ul> ? ? <li v-for="(item, index) in persons" :key="index"> ?? ? ? ? ?序號:{{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,因為delete是系統(tǒng)關鍵字) ? ? ? del(index) { ? ? ? ? this.persons.splice(index, 1)? ? ? ? } ? ? } ? }) </script>
替換(修改)
相當于是先刪除,再添加
<div id="demo"> ? <h2>測試: v-for 遍歷數(shù)組</h2> ? <ul> ? ? <li v-for="(item, index) in persons" :key="index"> ?? ? ? ? ?序號:{{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下標直接設置為0,len長度也設置為0,item傳入要添加的對象
<div id="demo"> ? <h2>測試: v-for 遍歷數(shù)組</h2> ? <ul> ? ? <li v-for="(item, index) in persons" :key="index"> ?? ? ? ? ?序號:{{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>
附加知識點
在v-for遍歷中,都需要聲明:key,那么:key的作用是什么呢?
答:vue和react都實現(xiàn)了一套虛擬DOM,使我們可以不直接操作DOM元素,只操作數(shù)據(jù)便可以重新渲染頁面。而隱藏在背后的原理便是其高效的Diff算法。vue和react的虛擬DOM的Diff算法大致相同,其核心是基于兩個簡單的假設:
- 假設1、 兩個相同的組件產(chǎn)生類似的DOM結構,不同的組件產(chǎn)生不同的DOM結構。
- 假設2、 同一層級的一組節(jié)點,他們可以通過唯一的id進行區(qū)分。
簡單的說, :key的作用主要是為了高效的更新虛擬DOM
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue學習筆記之指令v-text && v-html && v-bind詳解
這篇文章主要介紹了vue學習筆記之指令v-text && v-html && v-bind詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Vue3中實現(xiàn)代碼高亮的兩種方法(prismjs和highlight.js)
最近忙著開發(fā)自己的博客系統(tǒng),在做界面展示的時候,需要讓代碼高亮,于是經(jīng)過在網(wǎng)上查閱,發(fā)現(xiàn)有兩款比較好用的插件實現(xiàn)代碼高亮,分別是prismjs和highlight.js,下面我分別介紹下,方便給需要的同學參考2025-04-04electron-vue開發(fā)環(huán)境內存泄漏問題匯總
這篇文章主要介紹了electron-vue開發(fā)環(huán)境內存泄漏問題匯總,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10在vue項目中 實現(xiàn)定義全局變量 全局函數(shù)操作
這篇文章主要介紹了在vue項目中 實現(xiàn)定義全局變量 全局函數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Vue數(shù)據(jù)更新頁面卻沒有更新的幾種情況以及解決方法
我們在開發(fā)過程中會碰到數(shù)據(jù)更新,但是頁面卻沒有更新的情況,下面這篇文章主要給大家介紹了關于Vue數(shù)據(jù)更新頁面卻沒有更新的幾種情況以及解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06