首页 > 解决方案 > Nativescript Vue:从ListView中的itemTap获取数组中的项目

问题描述

我正在尝试在 ListView 中获取 itemTap 上的对象。我希望能够访问对象的nameand age。但我无法弄清楚如何在onItemTap函数中执行此操作

数据

listOfItems: [{name: "Tom", age: 55}, {name:"Greg", age: 32}]

模板

<ListView for="item in listOfItems" @itemTap="onItemTap($event)">
  <v-template>
    <Label :text="item.text" />
  </v-template>
</ListView>

方法

onItemTap: function(args) {
  ???
}

标签: nativescript-vue

解决方案


查看文档

Template

<ListView for="item in listOfItems" @itemTap="onItemTap">
  <v-template>
    <!-- Shows the list item label in the default color and style. -->
    <Label :text="item.text" />
  </v-template>
</ListView>

Methods

onItemTap(event) {
  console.log(event.index)
  console.log(event.item)
}

推荐阅读