Can I call mixin function from asyncData() method of the page component with Nuxt.js?
My code:
<template>
…
</template>
<script>
import api from “@/plugins/api/api.js”
…
export default {
…
async asyncData(context) {
…
context.apiMethodName()
…
}
…
}
…
</script>
api.js
import vue.js from ‘vue’
import API from ‘@/assets/js/api’
Vue.mixin({
methods: {
apiMethodName() { … }
}
})
Solution :
You cant call vue.js methods from withing asyncData, because asyncData executed before vue.js have an instance.
You can extract method into simple js function and call it both in asyncData and the vue.js method, but keep in mind that in asyncData you wont be able to access vue.js instance properties and other methods