Pu sh ax ios запросы к массиву ax ios .all - PullRequest
0 голосов
/ 04 февраля 2020

Спокойной ночи.

Я пытаюсь добавить массив в axios.all([]) массив!

Мой код:

app2. js

new Vue({
    el: '#central',
    data: {
        estilo: 'resize:none;text-align:center;color:red;width:450px;height:200px;font-size:15px;',
        capkey: 'text-align:center;color:RED;font-size:17px;;width:20%;height:40%;',
        ativar: true,
        buttonvalue: 'Inserir lista',
        livestyle: 'color:#519872;font-size:17px;',
        diestyle: 'color:#fd2eb3;font-size:17px',
        lives: [],
        dies: [],
        testar: axios.all([])
    },
    methods: {

        checkin(e) {
            console.log(this)
            this.buttonvalue = 'Testar'
            this.ativar = false
            var lista = e.split('\n');

            lista.map((value, key) => {

                this.testar.push(axios.get('http://localhost/fg/nova.php', {
                    crossDomain: true,
                    params: {
                        lista: value
                    }
                }));


            });

            this.testar.then(responseArr => {
                //code...
            });


        },
    }
})

Как добавить запросы (объект) в axios.all ([]), используя pu sh для массивов, а затем обработать их все параллельно?

vue.js:1897 TypeError: this.testar.push is not a function
    at app2.js:24

Спасибо!

Ответы [ 2 ]

0 голосов
/ 04 февраля 2020

axios.all([]) вернет обещание. Поэтому использование .push в обещании даст вам ошибку:

TypeError: this.testar.pu sh не является функцией

Вместо этого просто создайте массив без функции ax ios.

data: {
    ...
    testar: []
},

Внутри функции checkin измените map logi c на следующие правила. Это создаст новый массив с обещаниями и назначит новый массив свойству this.testar. Затем с помощью Promise.all вы можете дождаться одновременного разрешения всех обещаний.

var lista = e.split('\n');
this.testar = lista.map((value) =>

    axios.get('http://localhost/fg/nova.php', {
        crossDomain: true,
        params: {
            lista: value
        }
    });

);

Promise.all(this.testar).then(responseArr => {
    //code...
});

Если вы не хотите ждать окончания всех обещаний, прежде чем продолжить, вы можете удалить функцию Promise.all и добавьте ваши методы then к функции axios.get.

var lista = e.split('\n');
this.testar = lista.map((value) =>

    axios.get('http://localhost/fg/nova.php', {
        crossDomain: true,
        params: {
            lista: value
        }
    }).then(response => {
       // code...
    });

);
0 голосов
/ 04 февраля 2020

попробуйте

new Vue({
el: '#central',
data: {
    estilo: 'resize:none;text-align:center;color:red;width:450px;height:200px;font-size:15px;',
    capkey: 'text-align:center;color:RED;font-size:17px;;width:20%;height:40%;',
    ativar: true,
    buttonvalue: 'Inserir lista',
    livestyle: 'color:#519872;font-size:17px;',
    diestyle: 'color:#fd2eb3;font-size:17px',
    lives: [],
    dies: [],
    testar: []
},
methods: {

    checkin(e) {
        console.log(this)
        this.buttonvalue = 'Testar'
        this.ativar = false
        var lista = e.split('\n');

        lista.map((value, key) => {

            this.testar.push(axios.get('http://localhost/fg/nova.php', {
                crossDomain: true,
                params: {
                    lista: value
                }
            }));


        });

        axios.all(this.testar).then(axios.spread(...responseArr) => {
            //code... var x= responseArr[0]
        });


    },
}

})

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...