Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作的實(shí)現(xiàn)
語(yǔ)法結(jié)構(gòu):splice(index,len,[item])
1、可以用來(lái)添加/刪除/替換數(shù)組內(nèi)某一個(gè)或者幾個(gè)值
2、該方法會(huì)改變?cè)紨?shù)組
- index:數(shù)組開(kāi)始下標(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í)行刪除操作
<template> <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> </template> <script> export default { data(){ return{ 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> <style> </style>
二、替換(修改):
相當(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é)
到此這篇關(guān)于Vue中splice()方法對(duì)數(shù)組進(jìn)行增刪改等操作實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue splice()對(duì)數(shù)組增刪改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
antd的select下拉框因?yàn)閿?shù)據(jù)量太大造成卡頓的解決方式
這篇文章主要介紹了antd的select下拉框因?yàn)閿?shù)據(jù)量太大造成卡頓的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟
這篇文章主要介紹了vue-cli4.x創(chuàng)建企業(yè)級(jí)項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Vue打包后訪問(wèn)靜態(tài)資源路徑問(wèn)題
在本篇文章里小編給各位整理的是關(guān)于Vue打包后訪問(wèn)靜態(tài)資源路徑問(wèn)題相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-11-11vue-cli + sass 的正確打開(kāi)方式圖文詳解
本文通過(guò)圖文并茂的形式給大家介紹了vue-cli + sass 的正確打開(kāi)方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-10-10Vue3中的ref和reactive響應(yīng)式原理解析
這篇文章主要介紹了Vue3中的ref和reactive響應(yīng)式,本節(jié)主要介紹了響應(yīng)式變量和對(duì)象,以及變量和對(duì)象在響應(yīng)式和非響應(yīng)式之間的轉(zhuǎn)換,需要的朋友可以參考下2022-08-08