0

I'm using Laravel 5.3 with Vue.js(very new to this). Here's my current code

app.js

var vm = new Vue({
    el: '#app',

    data:  {
            messages: []
    },

    ready: function(){

        this.getMessages();
    },


    methods: {

        getMessages: function(){
                this.$http.get('api/messages').then((response) => {
                    this.$set('messages', data);
                        }, (response) => {
                });
        }
    }
});

api.php route is very simple

Route::get('/messages', function() {
     return Message::latest()->get();
    });

Note: here when i try access the route directly as localhost:8000/api/messages i get the array with the full data

On my view i have

<div class="content"   id="app">
                <tr v-for="message in messages">
                    <td> @{{ message}} </td>
                </tr>
</div>

I have included online libraries for all jquery, vue.js, and vue.resource. When i use vue.js debugger it shows that it returns messages[] but it's empty. I have followed a lot of examples but couldn't get it to work. Any help is greatly appreciated

3 Answers 3

2

if you are using vue.js 2.0 , ready is deprecated now, you may use mounted instead

mounted: function () {
  this.$nextTick(function () {
    this.getMessages();
  })
}

Vue.js Docs

Sign up to request clarification or add additional context in comments.

Comments

1

Since you are using the arrow syntax, then I switched to full ES2015 Code

getMessages() {

    this.$http.get('api/messages')
      .then( result => {
       this.messages = result.json() 
      })

}

2 Comments

Belmin. Thanks so much for your response. i tried and it still returned empty. but i kind of find a solution perhaps not the best. I was using vue.js version 2.0.1, once i changed it to older version 1.0.2 it worked.
Unfortunately I didn't work with VueJS 2.0 yet, so probabbly something in API is changed.
0

Try this:

var vm = new Vue({
    el: '#app',

    data:  {
            messages: []
    },

    ready: function(){

        this.getMessages();
    },


    methods: {

        getMessages: function(){
                let ctrl = this;
                this.$http.get('api/messages').then((response) => {
                    this.messages = response.data;
                });
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.