所以我加载了我的组件,然后我调用了类似下面的内容:
created() {
this.$store.dispatch('messages/connect');
this.$store.dispatch('messages/fetchAllMessages');
// this.$emit('set-recipient', this.chats[0]);
},
computed: mapGetters('messages', {
chats: 'getMessages'
}),
创建的注释部分是我想要运行的片段,但仅限于创建this.chats
而不是之后的任何更新。
如果我尝试发出当前的事件,我会收到错误:Cannot read property '0' of null
。
希望你明白我的意思。
有任何想法吗?
qazxsw poi打电话给你的服务器来获取消息,对吗?当meit像这样运行时,异步进程不会被完成。
如果你确定从添加了fetchallMessages
后解决的那个动作返回一个Promise,你可以这样做:
chats
如果您无法从该操作返回Promise,请分享其实现,我们将对其进行修复。
如果我理解正确,你只想在this.$store.dispatch('messages/fetchAllMessages')
.then(() => {
this.$emit('set-recipient', this.chats[0]);
})
初始化后执行this.$emit('set-recipient', this.chats[0]);
。
你有2个选择:
chats
作为聊天getter,只需定义自己计算:
mapGetters