repeat 的 event 问题
浏览器端, 在 repeat 元素上 使用 onclick 无法传递值
<div class="nav-item-cell" repeat="navList">
<text class="nav-item {{nav == value ? 'nav-active' : ''}}" id="nav{{value}}" onclick="switchNav(value)">
{{label}} ({{count}})
</text>
</div>
这里 除了 onclick 里面的 value 在 switchNav 函数中拿不到外,其他的表现都正常。 START WEEX HTML5: 0.4.0
确实有这个问题。我在你代码上稍作修改,以下demo可以直接运行测试。
这个写法会打印 undefined
<template>
<div class="ct">
<div class="nav-item-cell" repeat="navList">
<text onclick="switchNav(value)">
{{value}}
</text>
</div>
</div>
</template>
<script>
module.exports = {
data: {
navList: [{value: 'foo'}, {value: 'bar'}]
},
methods: {
switchNav: function(val) {
console.log('switchNav:', val)
}
}
}
</script>
这个写法会直接报错:
<template>
<div class="ct">
<div class="nav-item-cell" repeat="item in navList">
<text onclick="switchNav(item.value)">
{{item.value}}
</text>
</div>
</div>
</template>
<script>
module.exports = {
data: {
navList: [{value: 'foo'}, {value: 'bar'}]
},
methods: {
switchNav: function(val) {
console.log('switchNav:', val)
}
}
}
</script>
原因可能是之前一个对vm._realParent的赋值的pr导致的,我们会进一步排查这个问题,感谢反馈。@lmnsg
https://github.com/alibaba/weex/pull/1284 相关 pr 目测是这个导致的 bug , value 值存在了 vm 里而 _realParentVm 里没有这个值. @Hanks10100 @Jinjiang 关注一下这个问题
@MrRaindrop 是这样的, 感谢!
https://github.com/alibaba/weex/pull/1284 这个pr修复 repeat 内添加事件上下文不一致的情况,但同时导致了 repeat 内的事件获取不到数据的情况。
这里有一个矛盾, 保持上下文一致或者不一致都有问题。综合考虑之后的解决方案:
- 保持原有设计, 些问题可以修复
- 如果用户发现上下文不一致,可以能过
this._realParent获取真正的上下文。
pr: https://github.com/alibaba/weex/pull/1501