Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call mixin function from asyncData() method of the page component with Nuxt.js

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 from 'vue'
import API from '@/assets/js/api'

Vue.mixin({
  methods: {
    apiMethodName() { ... }
  }
})
like image 222
Dmytro Zarezenko Avatar asked Mar 04 '23 08:03

Dmytro Zarezenko


1 Answers

You cant call vue methods from withing asyncData, because asyncData executed before vue have an instance.

You can extract method into simple js function and call it both in asyncData and your vue method, but keep in mind that in asyncData you wont be able to access vue instance properties and other methods

like image 196
Aldarund Avatar answered Apr 24 '23 01:04

Aldarund