0đź‘Ť
fetchallMessages
calls your server to get the messages, right? that asynchonous process won’T be finshed when the meit is run like that.
If you make sure to return a Promise from that action which resolves after you have added chats
, you can do this:
this.$store.dispatch('messages/fetchAllMessages')
.then(() => {
this.$emit('set-recipient', this.chats[0]);
})
If you have trouble returning a Promise from that action, share its implementation and we’ll fix it.
- [Vuejs]-How do I define a component in a slot which as a prop which is defined in the child component
- [Vuejs]-Vue.js input field still accepts character input
0đź‘Ť
If i understand correctly, you want to execute this.$emit('set-recipient', this.chats[0]);
only after chats
was initialized.
You have 2 options:
-
don’t use
mapGetters
for the chats getter, just define the computed yourself:computed: { ...mapGetters('messages') chats(){ const messages = this.$store.getters.getMessages; if (messages.length){ this.$emit('set-recipient', this.chats[0]); } return messages; } }
-
Instead of doing it in the component, you can move the logic to the store and emit the event from there when you modify chats
Source:stackexchange.com