简体   繁体   English

计算内部的Axios连接

[英]Axios connection inside of computed

I'm trying to access my mysql db inside of computed. 我正在尝试访问我的mysql数据库。 I need to wait for my orderList variable to get updated. 我需要等待我的orderList变量得到更新。 Well i kinda make things work but the thing is it should only update my orderList when my filter variable changed. 好吧,我有点使事情起作用,但问题是,仅当我的过滤器变量更改时,它才应该更新我的orderList Instead it updates it one after another. 而是依次更新它。 Here is the code: 这是代码:

computed: {
    filteredItems: function() {

       if (this.filter === '') {
            return this.orderList
        }

        let myFirstPromise = new Promise((resolve, reject) => {

            axios.get('/api/admin/order/filter/' + this.filter)
                .then(response => {
                    if (response.data.error !== undefined) {
                        this.curView = 'error'
                        this.error = response.data.error
                        return
                    }
                    this.curView = 'pageOrderlist'
                    response.data.data.forEach(e => { e.cur = 'loading' })
                    resolve(response.data.data); // Yay! Everything went well!
                })
        });

        myFirstPromise.then((successMessage) => {
            this.orderList = successMessage
        });

        return this.orderList 
    }

calls it after and after again What am i missing? 一遍又一遍地打电话给我,我想念什么? Can you guys help me out please? 你们能帮我吗?

Use watch on filter variable and if it is empty then execute the axios from methods. filter变量上使用watch ,如果为空, axios方法中执行axios

Meaning, it will watch the filter variable whether it is empty or not, and if it finds it empty it will execute axios api request. 意思是,它将监视过滤器变量是否为空,如果发现它为空,它将执行axios api请求。

i think, you do not need promises, and also do not use computed. 我认为,您不需要承诺,也不需要使用计算。

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

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