在Vuejs中,在子组件上使用哪个生命周期挂钩在父组件之后运行



在父级上,我正在运行以下命令:

mounted(){
this.$store.dispatch('fetchNotesAction')
},
components: { ChildComponent }

这样,Vuex存储中就会填充数据。

因此,我假设,当子组件运行时,存储区已经满了。

ChildComponent中,我正在尝试这个

mounted(){
console.log(this.$store.getters.getNotes) 
},

如果我在父级中记录完全相同的代码,则在dispatch之后会显示数据。因此,这些行后面的代码(连接(运行良好,只是我假设不需要再次运行(从fetchNotesAction(fetch是否正确?

我尝试了created而不是mounted,还有同样的

最佳实践是使用一个返回状态getter的计算属性,然后使用watch属性来观察其在子组件中的变化:

computed:{
notes(){
return  this.$store.getters.getNotes;
}
},
watch:{
notes(newval,oldVal)'
console.log(newval) 
}
}

相关内容

最新更新