这段代码依旧存在问题,当点击.c_item时无法执行“getSearch”方法内的内容 getSearch内容如下getSearch(e){ let index = e.currentTarget.dataset console.log(index) let searchContent = this.hotSearch[index] console.log(searchContent) },请你修改这段代码
时间: 2024-03-24 08:41:56 浏览: 14
在这段代码中,获取 index 的方式不太正确,应该使用 `e.currentTarget.dataset.index` 来获取。另外,由于 `this.hotSearch` 是一个数组,应该使用 `this.hotSearch[index.index]` 来获取对应的值。修改后的代码如下:
```
<view class="flex-row list">
<view v-for="(item, index) in hotSearch" :key="index">
<view class="c_item color" :data-index="index" @click="getSearch">{{item}}</view>
</view>
</view>
<script>
export default {
data() {
return {
hotSearch: ['apple', 'banana', 'orange']
}
},
methods: {
getSearch(e) {
let index = e.currentTarget.dataset.index
console.log(index)
let searchContent = this.hotSearch[index]
console.log(searchContent)
}
}
}
</script>
```
这样就可以正确地输出点击的索引以及对应的值了。