淺析vue.js數(shù)組的變異方法
Vue 包含一組觀察數(shù)組的變異方法,所以它們也將會觸發(fā)視圖更新。這些方法如下:
- push()
- pop()
- shift()
- unshift()
- splice()
- sort()
- reverse()
都有什么功能?動手試驗(yàn)了一下:
<body> <div id="app"> <div> push方法: <input type="text" v-model="text" @keyup.enter="methodByPush"> <input type="button" value="測試功能" @click="methodByPush"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> <div> pop方法: <input type="button" value="測試功能" @click="methodByPop"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> <div> shift方法: <input type="button" value="測試功能" @click="methodByShift"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> <div> unshift方法: <input type="text" v-model="text" @keyup.enter="methodByUnshift"> <input type="button" value="測試功能" @click="methodByUnshift"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> <div> splice方法: <input type="button" value="測試功能" @click="methodBySplice"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> <div> sort方法: <input type="button" value="測試功能" @click="methodBySort"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> <div> reverse方法: <input type="button" value="測試功能" @click="methodByReverse"> <ul> <li v-for="item of items"> <span v-text="item"></span> </li> </ul> </div> result顯示的地方:<br> <span v-text="result"></span> </div>
<script> var vm = new Vue({ el: '#app', data: { items: [], text: '', result: '' }, methods: { methodByPush: function () { this.result = this.items.push(this.text) this.text = '' }, methodByPop: function () { this.result = '' this.result = this.items.pop() }, methodByShift: function () { this.result = '' this.result = this.items.shift() }, methodByUnshift: function () { this.result = '' this.result = this.items.unshift(this.text) this.text = '' }, methodBySplice: function () { this.result = '' this.result = this.items.splice(2,1,'yovan') }, methodBySort: function () { this.result = '' this.result = this.items.sort() }, methodByReverse: function () { this.result = '' this.result = this.items.reverse() alert(this.result) } } }) </script>
得到下面的結(jié)論:
push() 往數(shù)組最后面添加一個元素,成功返回當(dāng)前數(shù)組的長度
pop() 刪除數(shù)組的最后一個元素,成功返回刪除元素的值
shift() 刪除數(shù)組的第一個元素,成功返回刪除元素的值
unshift() 往數(shù)組最前面添加一個元素,成功返回當(dāng)前數(shù)組的長度
splice() 有三個參數(shù),第一個是想要刪除的元素的下標(biāo)(必選),第二個是想要刪除的個數(shù)(必選),第三個是刪除
后想要在原位置替換的值(可選)
sort() 使數(shù)組按照字符編碼默認(rèn)從小到大排序,成功返回排序后的數(shù)組
reverse() 將數(shù)組倒序,成功返回倒序后的數(shù)組
后來發(fā)現(xiàn)這些應(yīng)該都是javascript本來的方法吧?以前javascript沒學(xué)好,正好趁這次把這些方法的用法都給撿回來!
相關(guān)文章
vuex2中使用mapGetters/mapActions報(bào)錯的解決方法
這篇文章主要介紹了vuex2中使用mapGetters/mapActions報(bào)錯解決方法 ,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10vue3中安裝使用vue-i18n實(shí)時切換語言且不用刷新
這篇文章主要介紹了vue3中安裝使用vue-i18n實(shí)時切換語言不用刷新問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04vueJs函數(shù)toRaw?markRaw使用對比詳解
這篇文章主要為大家介紹了vueJs函數(shù)toRaw?markRaw使用對比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03