I'm using Nuxt with Vue Router and Axios. I see Vue Router has this fantastic feature called Navigation Guards.
Unfortunately, in the example below, my beforeRouteEnter()
function is called but seems to exit and switch pages before my manual next()
method is called in fetchPageData(next)
.
What is the correct pattern here?
export default {
beforeRouteEnter (to, from, next) {
next(vm => {
vm.fetchPageData(next);
});
},
methods: {
async fetchPageData(next) {
const result = await this.$axios.$get('/api/v2/inventory/3906?apiKey=f54761e0-673e-4baf-86c1-0b85a6c8c118');
this.$store.commit('property/setProperty', result[0]);
next();
}
}
}
I assume that my first call to next(vm => {})
is running asynchronously, allowing execution to continue, resulting in a page change before I (most likely incorrectly) try to callback next().