uni-app 小程序端 v-for循环绑定事件@event="func(item)"传值触发时打印item为undefined;
小程序web开发者工具版本最新版本 RC v1.02.1906141;
HBuilderX最新正式版本2.01.2019.06.14;
**小程序端v-for循环一段数据的时候绑定事件传值item触发之后打印item为undefined;
例如最简单的v-for一个数字<view v-for="n in 10" :key="n" @tap="testTap(n)">{{ n }}</view>触发testTap打印n报错;**
发现v-for绑定key无效(该key不存在)的时候打印item值为undefined
现在的问题就是v-for一个数字v-for="n in 10" 触发绑定事件打印n的时候直接报错不是很理解,错误内容如下:
VM4479:1 thirdScriptError
Cannot read property '4' of undefined; [Component] Event Handler Error @ pages/cost-list/cost-list#handleEvent
TypeError: Cannot read property '4' of undefined
at http://127.0.0.1:48464/appservice/common/vendor.js:2773:27
at Array.forEach (<anonymous>)
at getExtraValue (http://127.0.0.1:48464/appservice/common/vendor.js:2760:22)
at http://127.0.0.1:48464/appservice/common/vendor.js:2828:37
at Array.forEach (<anonymous>)
at processEventExtra (http://127.0.0.1:48464/appservice/common/vendor.js:2811:15)
at processEventArgs (http://127.0.0.1:48464/appservice/common/vendor.js:2860:22)
at http://127.0.0.1:48464/appservice/common/vendor.js:2937:41
at Array.forEach (<anonymous>)
at http://127.0.0.1:48464/appservice/common/vendor.js:2919:23
相关代码如下:
testData: [
{ id: 1, title: '测试1' },
{
id: 2,
title: '测试2'
},
{
id: 3,
title: '测试3'
}
]
<view class="" v-for="n in 10" :key="n" @tap="testTap(n)">{{ n }}</view>
<view class="" v-for="item in testData" :key="index" @tap="testTap2(item)">{{ item.title }}</view>
testTap(item) {
console.log(item);
直接报错: //VM3859:1 thirdScriptError
Cannot read property '2' of undefined; [Component] Event Handler Error @ pages/cost-list/cost-list#handleEvent
TypeError: Cannot read property '2' of undefined
},
testTap2(item) {
console.log(item);
打印 //undefined
},
wow老花椒
感谢,解决了。原本我使用 :key="item.id" 报这个错误,但是 data-id="item.id" 中有值。按照你这个方法把 :key="item.id" 改成 :key="index" 就好了。真是蛋疼
2019-10-23 10:43