Jayjay
Jayjay

Reputation: 103

Cannot fetch the return value in using axios in Vue.js

How can i fetch the return value using this code.

axios.get('/task')
    .then(response => {
        this.tasks = response.data.tasks;
        return this.tasks;
});

It is a possible with this code or is any other means on how I can retrieved the value. I cannot retrieved tha value using the return.

Thanks in advance.

Upvotes: 0

Views: 422

Answers (2)

user9246109
user9246109

Reputation:

I suggest you to read about Vue's principle concepts such as its data reactivity system: https://v2.vuejs.org/v2/guide/reactivity.html

By setting local state like you do (this.tasks = response.data.tasks;), view elements react to data changes automagically and render accordingly.

For example, if you have a bunch of tasks that should be rendered, you could have something like this:

<template>
  <div class="tasks">
    <div v-for="task in tasks" class="task">{{ task }}<div>
  </div>
</template>

Whenever the HTTP request finishes and you set this.tasks = response.data.tasks, all of the tasks are automatically rendered. To answer your question: there is really no need to return after the request completes.

Upvotes: 1

Gautam Patadiya
Gautam Patadiya

Reputation: 1411

Have you tried like this?

let base = this;
axios.get('/task')
    .then(response => {
        base.tasks = response.data.tasks;
        return base.tasks;
});

Upvotes: 0

Related Questions