1

My returned data is an array of objects with a nested object. I'm unable to display the 'events' in a v-for loop in my template as I cannot seem to access that part of the returned data.

The data is returned like this (from Vue DevTools):

list: Object
    user: "name"
    id: "id"
    events: Array[3]
        0: Object
           title: "event 1"
        1: Object
           title: "event 2"
        2: Object
           title: "event 3"

Using Vue-resource (via CDN) how do I get display just the events in my template?

Template:

<template id="events-template">
  <div v-for="events in list">
    @{{events.title}}
  </div>    
</template>

My application:

Vue.component('events', {
template: '#events-template',

data: function() {
    return {
        list: []
    }
},

created: function() {
    this.fetchEventsList();
},

methods: {
    fetchEventsList: function() {
        this.$http.get('events', function(events) {
            this.list = events;
        }).bind(this);
    }
}

});
2
  • can you add the json of the list variable to your question? Commented Jan 31, 2016 at 19:21
  • @Fiete I'm not entirely sure what you mean; however I've rewritten the first code block to match the exact response shown in Vue DevTools Commented Jan 31, 2016 at 19:37

1 Answer 1

3

Ok it seems you try to access the false properties in your loop.

the list variable does not contain a list/array (object). The array you want to iterate is the events attribute of the list object. so list.events

Furthermore you want to access the property (title) of the "current" item (event) in the loop. Then you don't have to access to the hole array but rather to the current element. event.title

Replace your template block:

<template id="events-template">
    <div v-for="event in list.events">
        @{{event.title}}
    </div>    
</template>
Sign up to request clarification or add additional context in comments.

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.