1

I'm trying to reuse a method in multiple vuejs component. I'm using mixin to do this but async method is not working

I have a async method in mixin that wait for an axios call GET-AllDrafts to finish and then run a mutation that updated the draft state value to the returned data from axios call.

async orgUpdateMixin() {
      await this.$store.dispatch('GET_AllDrafts')
                       .then(data => {                                       
                           this.$store.commit('draftUpdate', data);    //mutation                                              
                     })
}

I'm running this method in mounted() method of my component.

mounted(){
   this.orgUpdateMixin();
   console.log(this.data);
}

But for some reason this.data is returning [] empty array. Is there a way to wait for mixin to finish first before continuing.

1
  • 3
    What about doing await this.orgUpdateMixin();? Commented Sep 6, 2019 at 20:41

1 Answer 1

1

As commented, do:

async mounted() {
  await this.orgUpdateMixin();
  console.log(this.data);
}
Sign up to request clarification or add additional context in comments.

1 Comment

mounted cannot be async by vuejs v2.6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.