vue3 數(shù)組清空與重新賦值的操作代碼
vue3 數(shù)組清空與重新賦值
vue3里面,如果數(shù)組是用reactive()聲明的,要清空數(shù)組得用list.length = 0,如果想要使用list =[],或者直接賦值類型list = [1,2,3,4,5],得把數(shù)組用ref([])來聲明,然后用list.value = []來修改,然后如果是對象里面的數(shù)組,可以直接使用obj.list = []來清空,因為obj已經(jīng)被響應(yīng)式了.
<template> <div> {{list}} <button @click="add">增加</button> <button @click="clearAll">清空</button> {{newList}} </div> </template> <script> import { ref, watch, toRefs,computed,reactive,onBeforeUpdate,onUpdated,onBeforeMount,onMounted,onBeforeUnmount,onUnmounted ,onRenderTracked,onRenderTriggered } from 'vue' export default { name: '', components: { }, setup(){ let list = ref([]) function add(){ list.value.push(123) state.newList.push(123) } let state = reactive({ newList:[] }) function clearAll(){ list.value = [1,2,3,4,5] // list.length = 0 state.newList = [] console.log(list) } return { list, add, clearAll, ...toRefs(state) } }, } </script>
清空數(shù)組的幾個方式介紹
1. 前言
前兩天在工作當(dāng)中遇到一個問題,在 vue3
中使用 reactive
生成的響應(yīng)式數(shù)組如何清空,當(dāng)然我一般清空都是這么寫:
let array = [1,2,3]; array = [];
不過這么用在 reactive
代理的方式中還是有點問題,比如這樣:
let array = reactive([1,2,3]); watch(()=>[...array],()=>{ console.log(array); },) array = reactive([]);
很顯然,因為丟失了對原來 響應(yīng)式
對象的引用,這樣就直接失去了 監(jiān)聽
。
2. 清空數(shù)據(jù)的幾種方式
2.1 使用ref()
使用 ref
,這是最簡便的方法:
const array = ref([1,2,3]); watch(array,()=>{ console.log(array.value); },) array.value = [];
2.2 使用slice
slice
顧名思義,就是對數(shù)組進(jìn)行 切片
,然后返回一個新 數(shù)組
,感覺和 go
語言的 切片
有點類似。當(dāng)然用過 react
的小伙伴應(yīng)該經(jīng)常用 slice
,清空一個數(shù)組只需要這樣寫:
const array = ref([1,2,3]); watch(array,()=>{ console.log(array.value); },) array.value = array.value.slice(0,0);
不過需要注意要使用 ref
。
2.3 length賦值為0
個人比較喜歡這種,直接將 length
賦值為 0
:
const array = ref([1,2,3]); watch(array,()=>{ console.log(array.value); },{ deep:true }) array.value.length = 0;
而且,這種只會觸發(fā)一次,但是需要注意 watch
要開啟 deep
:
有的語言可能不支持。
不過,這種方式,使用 reactive
會更加方便,也不用開啟 deep
:
const array = reactive([1,2,3]); watch(()=>[...array],()=>{ console.log(array); }) array.length = 0;
2.4 使用splice
副作用
函數(shù) splice
也是一種方案,這種情況同時也可以使用 reactive
:
const array = reactive([1,2,3]); watch(()=>[...array],()=>{ console.log(array); },) array.splice(0,array.length)
不過要注意, watch
會觸發(fā)多次:
當(dāng)然也可以使用 ref
,但是注意這種情況下,需要開啟 deep
:
const array = ref([1,2,3]); watch(array,()=>{ console.log(array.value); },{ deep:true }) array.value.splice(0,array.value.length)
但是可以看到 ref
也和 reactive
一樣,會觸發(fā)多次。
3. 總結(jié)
以上是我個人 工作中
的對于 清空數(shù)組
的總結(jié),但是可以看到 splice
還是有點特殊的,會觸發(fā)多次,不過為什么會產(chǎn)生這種差異還有待研究。
到此這篇關(guān)于vue3 數(shù)組清空與重新賦值的文章就介紹到這了,更多相關(guān)vue 數(shù)組清空與重新賦值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element中table操作按鈕展示與折疊的實現(xiàn)示例
因為隨著功能的增多,table操作欄中的功能按鈕增多,這時候就需要折疊,本文主要介紹了element中table操作按鈕展示與折疊的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2022-04-04使用webpack打包后的vue項目如何正確運行(express)
這篇文章主要介紹了使用webpack打包后的vue項目如何正確運行(express) ,接下來通過本文給大家詳細(xì)介紹,需要的朋友可以參考下2018-10-10vue.js動態(tài)修改background-image問題
這篇文章主要介紹了vue.js動態(tài)修改background-image問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08vue指令之表單控件綁定v-model v-model與v-bind結(jié)合使用
這篇文章主要介紹了vue指令之表單控件綁定v-model v-model與v-bind結(jié)合使用,需要的朋友可以參考下2019-04-04elementui的el-popover修改樣式不生效的解決
在使用element-ui的時候,有一個常用的組件,那就是el-popover,本文就介紹一下elementui的el-popover修改樣式不生效的解決方法,感興趣的可以了解一下2021-06-06vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作
這篇文章主要介紹了vue添加錨點,實現(xiàn)滾動頁面時錨點添加相應(yīng)的class操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08