Jim E Russel
Jim E Russel

Reputation: 495

vue - Pass index to method onLoad

Is there a way to pass a v-for index to a method on page load? The common way I pass index is using @change event with a select drop down, but this time I'm creating a report without a select drop down.

<div v-for="(item, index) in items">
    <span>{{item.name}}</item>
    @onLoad='getData(index)'
</div>

methods: {
    getData(index) {
      axios.get('api', {
        params: { id: this.items[index].item_id}
      })
      .then({response=>})
    }
  }

Upvotes: 0

Views: 256

Answers (1)

Nikola Kirincic
Nikola Kirincic

Reputation: 3757

Use the mounted() component's function to populate items on the component load.

mounted()
{

    this.items.forEach((item, index) => {
        axios.get('api', {
            params: {id: this.items[index].item_id}
        })
            .then(response => {

                item.updated_property = response.data;
                Vue.set(this.items, index, item);
     });

    });
}

Upvotes: 1

Related Questions