Ensure that Vuex state is loaded before render component
For this particular case it was just going back and forth to the same vue instance. Solved it by adding :key="some-unique-key"
, so it looks like this.
<template>
<main class="main">
<AddUser :key="$route.params.userId"/>
<UserList/>
</main>
</template>
Although this solved, but I will answer for future comers. You may issue the dispatch
beforehand as Shu suggested, or you may still dispatch on same component mounted
hook, but use some state variable to track the progress:
data:{
...
loading:false,
...
},
...
mounted(){
this.loading = true,
this.$store
.dispatch('fetchUsers')
.finally(() => (this.loading=false));
}
Then in your template you use this loading
state variable to either render the page or to render some spinner or progress bar:
<template>
<div class='main' v-if="!loading">
...all old template goes her
</div>
<div class="overlay" v-else>
Loading...
</div>
</template>
<style scoped>
.overlay {
display: flex;
align-items: center;
justify-content: center;
z-index: 10;
color: #FFFFFF;
}
</style>
I would use beforeRouteEnter
in User.vue
so that the component is not initialized before the data is loaded.
(Assuming you are using vue-router
)
beforeRouteEnter (to, from, next) {
if (store.state.users.length === 0) {
store.dispatch(fetchUsers)
.then(next);
}
},
You'll need to
import store from 'path/to/your/store'
because this.$store
is not available until the component is initialized.