laravel-5 - Vue Js - axios 问题使用 laravel 执行多个并发请求

标签 laravel-5 laravel-blade vue-component vue.js

好吧,我可能有一些小问题,但我想不通。

如果我使用 axios 只传递一个 http 请求,一切正常。

但是当我使用多个请求时,我在控制台日志中得到结果但无法发送到 View 文件(laravel blade View 文件)。

让我给你看我的代码:

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'

})

谁能告诉我如何在我的 View 文件中显示数据?

最佳答案

发生这种情况是因为 axios.spreadthis 的范围不正确,早些时候它对你有用,因为你使用的是自动绑定(bind)的 es6 箭头函数这个范围。

您可以进行以下更改以使其工作:

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;
            }
    ))

}

关于laravel-5 - Vue Js - axios 问题使用 laravel 执行多个并发请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41629098/

相关文章:

javascript - Vue : Input Form in a component doesn't return a value

laravel - 手动创建分页器 (Laravel 5)

php - Laravel 5.1 - 验证用户名、密码,如果确认 = 1

php - 拉维尔 : Class 'App\DB' not found

php - 我无法在 Laravel 中检索区域设置字符串

javascript - Vue js v-if 在共享切换按钮上进行条件渲染

vue.js - 如何将组件数据作为json发送到服务器?

php - 添加自定义验证错误消息 laravel

javascript - 如何从 Blade 模板中的 Javascript 文件重定向 Laravel 路由

php - Laravel - Blade 注释, Blade 渲染导致页面崩溃