Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vuejs Axios data not showing

The problem of not showing information is delayed in fetching , i need any help for this problem.

        <h1>@{{message}}</h1>

        <div class="panel panel-primary">
            <div class="panel-heading">
                <div class="row">
                    <div class="col-md-10"><h3 class="panel-title">Experience</h3></div>
                    <div class="col-md-2 text-right">
                        <button class="btn btn-success">Ajouter</button>
                    </div>
                </div>

            </div>
            <div class="panel-body" >

                <ul class="list-group">
                    <li class="list-group-item" v-for="experience in experiences" >
                        <div class="pull-right">
                            <button class="btn btn-warning btn-sm">Editer</button>
                        </div>
                        <h3>@{{experience.titre}}</h3>
                        <p>@{{experience.body}}</p>
                        <small>@{{experience.debut}} - @{{experience.fin}}</small>
                    </li>

                </ul>

            </div>
        </div>

Vuejs

<script src="{{asset('js/vue.js')}}"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
     var app = new Vue({
       el: '#app',
       data: {
         message: 'Nawfal Kharbouch',
         experiences:[]
       },
       methods:{
        getExperiences:function(){
            axios.get('http://localhost:8080/getexperiences').then(response=>{
                this.experiences=response.data;
                console.log(this.experiences);
            }).catch(function(error){
                console.log('erros : ',error);
            })  
        }
       },
       mounted:function(){
        this.getExperiences();
        console.log(this.experiences);
        }
    })
</script>

The problem of not showing information is delayed in fetching , i need any help for this problem. //console Google Chrome

[__ob__: Observer]
vue.js:8553 You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html
backend.js:1  vue-devtools  Detected Vue v2.5.16 
5:171 (3) [{…}, {…}, {…}, __ob__: Observer];

//picture view vide

like image 265
Nawfal Kh Avatar asked Aug 01 '18 11:08

Nawfal Kh


2 Answers

Instead of using this directly you need to pass it to a variable.

methods:{
        getExperiences:function(){
        var vm = this
            axios.get('http://localhost:8080/getexperiences').then(response=>{
                vm.experiences=response.data;
                console.log(vm.experiences);
            }).catch(function(error){
                console.log('erros : ',error);
            })  
        }
       },
like image 72
Lucas G. Avatar answered Oct 18 '22 06:10

Lucas G.


You can not pass "this" inside a promise, you need to add "this" to a variable.

Exemple:

var app = new Vue({
       el: '#app',
       data: {
         message: 'Nawfal Kharbouch',
         experiences:[]
       },
       methods:{
        var self = this;
        getExperiences:function(){
            axios.get('http://localhost:8080/getexperiences').then(response=>{
                self.experiences=response.data;
                console.log(self.experiences);
            }).catch(function(error){
                console.log('erros : ',error);
            })  
        }
       },
        mounted:function(){
        this.getExperiences();
        console.log(this.experiences);
         }
    }) 
like image 33
Eduardo Paoli Avatar answered Oct 18 '22 05:10

Eduardo Paoli