【发布时间】:2021-10-28 19:18:34
【问题描述】:
鉴于下面的代码,我的子组件警报在父挂载函数中的任何代码之前触发。
因此,在数据准备好之前,孩子似乎已经完成了初始化,因此在重新加载之前不会显示数据。
当原始 JSON 显示在布局中的 v-card 内时,数据本身可以从 API 中正常返回。
我的问题是如何确保在子组件加载之前,父组件中请求的数据已准备好?我发现的所有内容都集中在使用 props 传入的静态数据上,但是当必须首先获取数据时,这似乎完全失败了。
在父级的mounted() 中,我有这段代码用于检索数据。
const promisesArray = [this.loadPrivate(),this.loadPublic()]
await Promise.all(promisesArray).then(() => {
console.log('DATA ...') // fires after the log in Notes component
this.checkAttendanceForPreviousTwoWeeks().then(()=>{
this.getCurrentParticipants().then((results) => {
this.currentP = results
this.notesArr = this.notes // see getter below
})
在父级中检索数据的getter
get notes() {
const newNotes = eventsModule.getNotes
return newNotes
}
我在父模板中的组件:
<v-card light elevation="">
{{ notes }} // Raw JSON displays correctly here
// Passing the dynamic data to the component via prop
<Notes v-if="notes.length" :notesArr="notes"/>
</v-card>
子组件:
...
// Pickingn up prop passed to child
@Prop({ type: Array, required: true })
notesArr!: object[]
constructor()
{
super();
alert(`Notes : ${this.notesArr}`) // nothing here
this.getNotes(this.notesArr)
}
async getNotes(eventNotes){
// THIS ALERT FIRES BEFORE PROMISES IN PARENT ARE COMPLETED
alert(`Notes.getNotes CALL.. ${eventNotes}`) // eventNotes = undefined
this.eventChanges = await eventNotes.map(note => {
return {
eventInfo: {
name: note.name,
group: note.groupNo || null,
date: note.displayDate,
},
note: note.noteToPresenter
}
})
}
...
我对 Vue 比较陌生,所以如果我忽略了一些基本的东西,请原谅我。我已经尝试修复它几天了,但无法弄清楚,因此非常感谢任何帮助!
【问题讨论】:
-
看来您使用的是vue-class-component ....正确?
标签: javascript vue.js components vue-props