I am new to vue-router navigation guards and so I recently realized that I needed to use beforeRouteUpdate
guard for reused components where for example: Going from /foo/1
to /foo/2
However, while coming to /foo/1
, I pulled data from database through an axios call and before going to /foo/2
, I need to pull new data again through the axios call.
This is where I face a problem where the navigation guard beforeRouteUpdate
loads the component /foo/2
before my data loads from the axios call and thus I get null in a few of my variables.
How can I make beforeRouteUpdate
wait to load the next component so that all my data is loaded from the axios calls?
As for my code, it looks like this:
beforeRouteUpdate (to, from, next) {
Vue.set(this.$store.state.user, 'get_user', null)
this.$store.dispatch(OTHER_PROFILE_GET, to.params.id).then(resp => {
console.log(resp);
if(this.$store.getters.is_user_loaded) {
next()
} else {
this.$store.watch((state, getters) => getters.is_user_loaded, () =>
{
if(this.$store.getters.is_user_loaded) {
console.log(this.$store.state.user.get_user);
console.log('comes here');
next()
}
})
}
})
},
To explain my code further, I have called this method in my component and so I when I go from /user/1
to /user/2
I dispatch a Vuex action which makes an axios call to get the new profile details but before the axios call completes and loads the data in the Vuex state, the beforeRouteUpdate
already loads the next component.
Vue.set
or otherwise) - Phil