示例代碼
<template>
<section class="courseDetail">
<p v-for="item in arr" :key="item.id">{{ item.obj.text }}</p>
</section>
</template>
<script setup>
const arr = ref([]) // 顯示的數(shù)據(jù)
const arr1 = ref([]) // 接口獲取的數(shù)據(jù)
let arr2 = ref([])
const updateArr = (list) => {
if (!list.value.length) {
arr2.value = []
} else {
arr2.value = list.value.map(item => {
return {
...item,
val: 1
}
})
}
}
const timer = setTimeout(() => {
arr1.value = [
{id:1, name: '數(shù)據(jù)1', obj: {val:1,text:'1'}},
{id:2, name: '數(shù)據(jù)2', obj: {val:1,text:'1'}}
]
updateArr(arr1)
arr.value = arr2.value
clearTimeout(timer)
}, 1000)
const timer2 = setTimeout(() => {
arr2.value[0].obj = {val:2,text:'2'}
updateArr(arr2)
console.log('arr2-2',arr2.value)
console.log('arr', arr.value)
clearTimeout(timer2)
}, 4000)
</script>