Dhaval
Dhaval

Reputation: 1436

Vue Js - issue with axios Performing multiple concurrent requests with laravel

alright I have small issue may be but I can't figure it out.

if I pass only one http request using axios all work good.

but when I use multiple request I get result in console log but can't able to send to view file ( laravel blade view file).

let me show you my code :

brand.blade.php

// main element for Vue js el: '#container'
<div id="container" class="row all_brands">
        <brands></brands>
    </div>


// template for brand component
<template id="brand-template">
   <ul>
     // if I only pass one http request through axios then  my data shows here but for multiple request not showing anything.
     <li v-for="brand in list" v-text="brand.brand_id"></li>

   </ul>
   @{{count}}
</template>

brand.js

Vue.component('brands',{

template: '#brand-template',

data: function(){

    return{

        list: [],
        count: ''
    }

},

created: function(){

    //axios.get('api/brands').then(response => this.list = response.data) ; // here I send only one gttp request and working find..

    axios.all([
    axios.get('api/brands'),
    axios.get('api/brand_count')
    ])
        .then(
        axios.spread(
            function (brand, count) {
             // this is not working and not set these data to my brand.blade.php file template
             this.list = brand.data;
             this.count = count.data;
             // console show me all the data that coming from http request  
            //console.log('list',brand.data);
            //console.log('count',count.data);
            }
    ))

}

});


new Vue({

el: '#container'

})

can any one let me how to show data in my view file ?

Upvotes: 1

Views: 2766

Answers (1)

Saurabh
Saurabh

Reputation: 73649

This is happening because scope of this is not correct in axios.spread, earlier it was working for you, as you were using es6 arrow function, which automatically binds this scope.

You can make following changes to make it work:

created: function(){
    var that = this    
    axios.all([
    axios.get('api/brands'),
    axios.get('api/brand_count')
    ])
        .then(
        axios.spread(
            function (brand, count) {
             that.list = brand.data;
             that.count = count.data;
            }
    ))

}

Upvotes: 5

Related Questions