dstm2014 2018-12-19 05:53
浏览 121
已采纳

从laravel控制器接收的数据未更新为vue变量

I am creating a web app having dashboard using laravel and vue. When I pass data from controller to vue file data is received properly but when I set it to vue variable the value is not set in the variable. All data is received and its displayed in the console but when I set it to the vue variable, the variable doesn't update its value. This is my Controller class:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
class UsersController extends Controller
{
    //
    public function index()
    {
        $users=User::all();

        return response()->json($users);
    }
}

This is myTeam.vue for receiving and displaying the data:

<template>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card card-default">
                    <div class="card-header">Example Component</div>


                    <h1>
                       This request list
                       Hello,{{this.items}}
                    </h1>

                    <ul class="list-group">
                        <li class="list-group-item" v-for="t in items">{{items}}</li>

                    </ul>

                      </div>
                </div>
            </div>
        </div>
</template>

<script>
    export default {
        data(){
            return {
                 //items: []
                 items:[],

            }
        },

        created() {
            var self=this;
             axios.get('/allusers').then((response) => self.items=response.data) .catch((error)=>console.log(error));

             axios.get('/allusers') .then(response => console.log(response.data));

            console.log('Component mounted.'+this.items)
        },


    }
</script>

Now when I run it the console prints the array properly means data is received but when I set it to items variable the data is not set.

My Output is this: This is the output image file

Please check it and thanks in advance ...

  • 写回答

1条回答 默认 最新

  • duanlie2709 2018-12-19 06:07
    关注

    This is never print items array because it's execute before the ajax response is filled.

        console.log('Component mounted.'+this.items)
    

    That's why your console is always blank. You can search about blocking and non blocking programming.

    your code have small bug. Update your code and try this:

    <h1>
     This request list
        Hello,{{items}}
    </h1>
    
    <ul class="list-group">
        <li class="list-group-item" v-for="t in items">{{t}}</li>
    </ul>
    ...
    
    <script>
        export default {
            data(){
                return {
                    items:[],
                }
            },
            mounted: function () {
                this.getList();
            }
    
            methods: {
                let _this = this;
                axios.get('/allusers')
                .then((response) => _this.items = response.data) 
                .catch((error)=>console.log(error));
            },
        }
    </script>
    

    This can help you. Good luck.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条