亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue?數(shù)組添加數(shù)據(jù)方式

 更新時間:2022年08月23日 16:15:55   作者:Now_li  
這篇文章主要介紹了vue?數(shù)組添加數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

vue 數(shù)組添加數(shù)據(jù)

數(shù)據(jù)添加分為三種方法

  • 1.unshift()
  • 2.push()
  • 3.splice()
<template>
? ?? ?<div>
?? ??? ?<ul>
?? ??? ? ?<li v-for="(time,index) of listTable" :key="index" @click="copyNew(time,index)">
?? ??? ? ? ?{{time.id}}{{time.name1}}{{time.name2}} 添加
?? ??? ? ?</li>
?? ??? ?</ul>
?? ?</div>
</template>
<script>
export default {
?? ? data(){
?? ? ? ?return{
?? ? ? ? ?listTable:[
?? ? ? ? ? ?{id:'1',name1:'a1',name2:'b1'},
?? ? ? ? ? ?{id:'2',name1:'a2',name2:'b2'},
?? ? ? ? ? ?{id:'3',name1:'a3',name2:'b3'},
?? ? ? ? ?],
?? ? ? ?}
?? ? ?},
?}
?</script>

1.unshift() //數(shù)組頭部添加一條新數(shù)據(jù)

let newList = {
? ?id:'4'
? ?name1:'a4',
? ?name2:'b4',
?}
this.listTable.unshift(newList) ?//unshift()在數(shù)組頭部添加一條數(shù)據(jù)?
console.log(this.listTable)

2.push() //數(shù)組末端添加一條新數(shù)據(jù)

this.listTable.push(newList) ?//push()在數(shù)組末端添加一條數(shù)據(jù)?
console.log(this.listTable)

3.splice() //數(shù)組操作

?copyNew(time,index){
? ?console.log(time)
? ?let newList = {
? ? ?id:time.id,
? ? ?name1:time.name1,
? ? ?name2:time.name2,
? ?}
? ?//第一個參數(shù)為需要操作數(shù)據(jù)的下標,第二個參數(shù)為操作添加/刪除(0為添加,1為不操作,2為刪除,3為刪除多條數(shù)據(jù)),第三個參數(shù)可選
? ?this.listTable.splice(index,0,newList)?
? ?console.log(this.listTable)
?}

4.concat() // 數(shù)組合并

let arrA = [1,2,3]
let arrB = [4,5]
arrA.concat(arrB) // 輸出 1,2,3,4,5
let arrC = [6,7]
arrA.concat(arrB,arrC) // 輸出 1,2,3,4,5,6,7

動態(tài)向數(shù)組中添加對象(關(guān)于v-for,input和push)

核心:深拷貝

第一步:

寫在data(): 設(shè)datas數(shù)組,以及datas中需求的對象

datas: [],
data_formInput: {
?? ?keyword: '',//關(guān)鍵字
?? ?describe: '',//描述
},

第二步:(對象中的屬性,input中的數(shù)據(jù))雙向綁定

<view class="box" v-show="box_show">
?? ?<view class="box_text">請輸入關(guān)鍵字</view><input type="text" v-model="data_formInput.keyword" />
?? ?<view class="box_text">請輸入描述</view><input type="text" v-model="data_formInput.describe" />
?? ?<button type="default" @click='create'>確定</button>
</view>

第三步:深拷貝保存數(shù)據(jù)并置空input

create() {
//這里要設(shè)一個對象來進行深拷貝才能避免每次push的時候都被最后一次提交的數(shù)據(jù)覆蓋,也可以避免置空的時候數(shù)據(jù)丟失
?? ?let obj = {
?? ??? ?keyword: this.data_formInput.keyword,
?? ??? ?describe: this.data_formInput.describe
?? ?}
?? ?this.datas.push(obj);
?? ?this.data_formInput.keyword = ''//提交input之后置空
?? ?this.data_formInput.describe = ''
},

第四步:循環(huán)顯示剛剛input提交的數(shù)據(jù)

<button type="default" v-for="(item,index) in datas" :key='index' @click='open(item.describe)'>
??? ? {{item.keyword}}
</button>

放一段完整代碼:

挺多的,實現(xiàn)了點擊添加關(guān)鍵字按鈕的時候打開輸入關(guān)鍵字和描述,提交的頁面,點擊提交的時候顯示已保存的關(guān)鍵字數(shù)據(jù)。邏輯很簡單,主要是記錄一下這里的深拷貝。

<template>
?? ?<view class="">
?? ??? ?
?? ??? ?
?? ??? ?<!-- 這里是輸入關(guān)鍵字和描述 -->
?? ??? ?<view class="box" v-show="box_show">
?? ??? ??? ?<view class="box_text">請輸入關(guān)鍵字</view><input type="text" v-model="data_formInput.keyword" />
?? ??? ??? ?<view class="box_text">請輸入描述</view><input type="text" v-model="data_formInput.describe" />
?? ??? ??? ?<button type="default" @click='create'>確定</button>
?? ??? ?</view>
?? ??? ?
?? ??? ?
?? ??? ?
?? ??? ?<!-- 這里顯示已提交的關(guān)鍵字以及添加關(guān)鍵字按鈕 -->
?? ??? ?<view v-show="button_show">
?? ??? ??? ?<button type="default" v-for="(item,index) in datas" :key='index'
?? ??? ??? ??? ?@click='open(item.describe)'>{{item.keyword}}</button>
?? ??? ??? ?<u-popup :show="show" @close="close" mode="center" round=20 closeable=true>
?? ??? ??? ??? ?<view>
?? ??? ??? ??? ??? ?{{show_describe}}
?? ??? ??? ??? ?</view>
?? ??? ??? ?</u-popup>
?? ??? ??? ?<button type="default" @click='open_box'>添加關(guān)鍵字</button>
?? ??? ?</view>
?? ??? ?
?? ??? ?
?? ?</view>
</template>
<script>
?? ?export default {
?? ??? ?data() {
?? ??? ??? ?return {
?? ??? ??? ??? ?datas: [],
?? ??? ??? ??? ?data_formInput: {
?? ??? ??? ??? ??? ?keyword: '', //關(guān)鍵字
?? ??? ??? ??? ??? ?describe: '', //描述
?? ??? ??? ??? ?},
?? ??? ??? ??? ?show_describe: '',
?? ??? ??? ??? ?show: false,
?? ??? ??? ??? ?box_show: false,
?? ??? ??? ??? ?button_show: true,
?? ??? ??? ?}
?? ??? ?},
?? ??? ?methods: {
?? ??? ??? ?create() {
?? ??? ??? ??? ?let obj = {
?? ??? ??? ??? ??? ?keyword: this.data_formInput.keyword,
?? ??? ??? ??? ??? ?describe: this.data_formInput.describe
?? ??? ??? ??? ?}
?? ??? ??? ??? ?this.datas.push(obj);
?? ??? ??? ??? ?this.data_formInput.keyword = ''//提交input之后置空
?? ??? ??? ??? ?this.data_formInput.describe = ''
?? ??? ??? ??? ?this.box_show = false
?? ??? ??? ??? ?this.button_show = true
?? ??? ??? ?},
?? ??? ??? ?// 打開描述
?? ??? ??? ?open(t) {
?? ??? ??? ??? ?this.show = true
?? ??? ??? ??? ?this.show_describe = t
?? ??? ??? ?},
?? ??? ??? ?close() {
?? ??? ??? ??? ?this.show = false
?? ??? ??? ?},
?? ??? ??? ?open_box() {
?? ??? ??? ??? ?this.box_show = true
?? ??? ??? ??? ?this.button_show = false
?? ??? ??? ?}
?? ??? ?}
?? ?}
</script>
<style scoped>
?? ?.box {
?? ??? ?width: 100%;
?? ??? ?height: 500rpx;
?? ??? ?overflow: hidden;
?? ??? ?/* margin-top: 50rpx; */
?? ??? ?background-image: url(https://pic.imgdb.cn/item/624c0962239250f7c58f97a2.png);
?? ??? ?background-repeat: no-repeat;
?? ??? ?background-size: cover;
?? ?}
?? ?.box_text {
?? ??? ?text-align: center;
?? ??? ?margin-bottom: 20rpx;
?? ?}
?? ?input {
?? ??? ?width: 80%;
?? ??? ?height: 30rpx;
?? ??? ?border: 1rpx solid black;
?? ??? ?margin-top: 50rpx;
?? ??? ?overflow: hidden;
?? ??? ?margin: 10rpx auto;
?? ??? ?padding-left: 20rpx;
?? ??? ?font-size: 25rpx;
?? ?}
</style>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評論