Rakesh K
Rakesh K

Reputation: 1320

How to return data inside computed property in axios request

i have a computed property and inside it has axios call. that computed propery i need to loop and show result using v-for.

my computed property looks like this.

  computed:{
        managers(){
       axios.post('/dispatcher/managers',{
                    'catId':this.catId,
                    'route':this.filters.selectedRoute,
                    'date':this.filters.selectedDate,
                    'manager':this.filters.selectedManager
                        }).then(response=>{
                           return response.data;
                        }).catch(err=>{})
            }

    }

When i try to loop managers using v-for it does not work.

How can i get it to work? Thanks.

Upvotes: 2

Views: 4063

Answers (1)

Bryan Downing
Bryan Downing

Reputation: 15472

According to the docs, you should probably use a watcher: https://v2.vuejs.org/v2/guide/computed.html#Watchers

In this case, using the watch option allows us to perform an asynchronous operation (accessing an API), limit how often we perform that operation, and set intermediary states until we get a final answer. None of that would be possible with a computed property.

Upvotes: 3

Related Questions