It is working in your computed property because it updates when one of its dependencies updates. In your case, it returns an empty array first and then updates after the users were fetched. Also, what does your this.data refer to, because it isn't declared anywhere ?
I would suggest that you use a getUsers getter in your Vuex module as it is a better practice than accessing the state directly. Then you can use it by declaring it as a computed :
computed: {
// Map necessary getters (add your namespace string as a first parameter of mapGetters if your module is namespaced)
...mapGetters(['getUsers']),
// Other computed properties
}
You can then use it as it was a data property. Now, it won't solve your issue that it is not set right off the bat because the API data has not been returned yet. Unfortunately, the API call is not instant, so you have to deal with it. There are many ways to do so. One of them would be using Vuex' subscribe functionality which is triggerred whenever a mutation is committed :
// In your component
data() {
return {
users: []
}
},
// ...
created() {
// Set a watcher on Vuex' mutations
this.unsubscribe = this.$store.subscribe((mutation, state) => {
// Rehydrate the users when an updateUsers mutation was processed inside the Vuex module
// Here, add your namespace if your module is namespaced : yourNamespace/updateUsers
if (mutation.type === 'updateUsers') {
this.users = this.getUsers;
}
});
},
beforeDestroy() {
// Unsubscribe when the component is going to be destroyed
this.unsubscribe();
}
P.S. This setup is good if you want to watch for multiple value changes. If users is the only value you want to watch, I suggest you use the Vuex' watch function instead.