简体   繁体   English

如何在axios请求中返回计算属性内的数据

[英]How to return data inside computed property in axios request

i have a computed property and inside it has axios call.我有一个计算属性,里面有 axios 调用。 that computed propery i need to loop and show result using v-for .我需要使用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.当我尝试使用v-for循环managers时,它不起作用。

How can i get it to work?我怎样才能让它工作? Thanks.谢谢。

According to the docs, you should probably use a watcher: https://v2.vuejs.org/v2/guide/computed.html#Watchers根据文档,您可能应该使用观察者: 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.在这种情况下,使用 watch 选项允许我们执行异步操作(访问 API),限制我们执行该操作的频率,并设置中间状态,直到我们得到最终答案。 None of that would be possible with a computed property.对于计算属性,这一切都不可能实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM