vue數組內數據改變時頁面不會響應而發生改變,需要使用root.$set(root是vue3.x對2.x的this的寫法)
root.$set(responseData,i,data[i]);
responseData是接收的數組對象,i是排序,data是從后端拿下來的數據
這個地方v-for=“i in responseData.length”的i是從1開始而不是0
后端獲取數據后的對象數組內屬性出現undefined
由於onMounted是在setup之后,也就是后端數據的獲取是在數據掛載之后,onMounted之前定義的const responseData = reactive([ {"name":""} ])
。所以responseData 是沒有以后的屬性的,比如理應v-for的接收數據的responseData是有一個name屬性的,但是responseData[1]卻會發生name is undefined
。這時候需要加上v-if判斷是否渲染:
<div v-for="index in responseData.length+1" :key="index">
<el-card shadow="hover" v-if="responseData[index]">
...
<el-card>
</div>
由1,數組的變化甚至不會觸發onUpdate和beforeUpdate
onMounted中調用的Promise函數在最后(異步)
onMounted(() => {
getTaken().then(response => {
console.log("foo")
})
.catch(error => {
console.log(error)
})
console.log("coo")
})
打印出:foo coo