Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v-for循环渲染中绑定事件传参index无效问题 #2673

Closed
Bulandent opened this issue Aug 5, 2020 · 2 comments
Closed

v-for循环渲染中绑定事件传参index无效问题 #2673

Bulandent opened this issue Aug 5, 2020 · 2 comments

Comments

@Bulandent
Copy link

如下代码所示,tapItem 传入的参数 index 会是 undefined,但是 <view>{{ index }}</view> 却能获取到值。

<view v-for="item in array">
    <view>{{ index }}</view>
    <view @tap="tapItem(index)"></view>
</view>

如果这个在上面代码的基础下在 v-for 中显示声明循环的索引 index,如下图代码。那么 tapItem 就能正常获取到参数 index 的值。

<view v-for="(item, index) in array">
    <view>{{ index }}</view>
    <view @tap="tapItem(index)"></view>
</view>

所以这应该是框架的一个 bug

@kt3721
Copy link

kt3721 commented Aug 5, 2020

通过查看编译后的代码发现,在事件中引用的参数会将 参数值 存储在 data-wpytap-a 等属性上。
image

在例一中:

  1. 事件参数 indexundefined,是因为 此时上下文中不存在 index 变量(改为例二中的写法后即可正常获取index)。
  2. 在模板中的 {{index}} 可以正常显示,是因为小程序默认的索引是 index,即使没有显式声明 index

所以使用例二的写法就可以了

@Gcaufy
Copy link
Collaborator

Gcaufy commented Aug 9, 2020

必须显示声明 index

@Gcaufy Gcaufy closed this as completed Aug 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants