提交 825aa771 编写于 作者: Q qiang

fix(mp): 修复 v-for 遍历复杂表达式时 stop 修饰符无效的问题

上级 f1861737
......@@ -346,6 +346,20 @@ describe('mp:compiler-extra', () => {
'<view><view class="item" v-for="i in \'abc\'" :key="i" @click="func(i)"></view></view>',
'<view><block wx:for="abc" wx:for-item="i" wx:for-index="__i0__" wx:key="*this"><view data-event-opts="{{[[\'tap\',[[\'func\',[\'$0\'],[[[\'#s#abc\',\'\',__i0__]]]]]]]}}" class="item" bindtap="__e"></view></block></view>'
)
assertCodegen(
'<view v-for="(item,index) in list" :key="index" @click.stop="func">{{item}}</view>',
'<block wx:for="{{list}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view data-event-opts="{{[[\'tap\',[[\'func\',[\'$event\']]]]]}}" catchtap="__e">{{item}}</view></block>'
)
assertCodegen(
'<view v-for="(item,index) in [1,2,3]" :key="index" @click.stop="func">{{item}}</view>',
'<block wx:for="{{[1,2,3]}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" catchtap="__e">{{item}}</view></block>',
'with(this){if(!_isMounted){e0=function($event){$event.stopPropagation();return func($event)}}}'
)
assertCodegen(
'<view v-for="(item,index) in array()" :key="index" @click.stop="func">{{item}}</view>',
'<block wx:for="{{$root.l0}}" wx:for-item="item" wx:for-index="index" wx:key="index"><view data-event-opts="{{[[\'tap\',[[\'e0\',[\'$event\']]]]]}}" catchtap="__e">{{item}}</view></block>',
'with(this){var l0=array();if(!_isMounted){e0=function($event){$event.stopPropagation();return func($event)}}$mp.data=Object.assign({},{$root:{l0:l0}})}'
)
assertCodegen(
'<view><view class="item" v-for="i in 5" :key="i" @click="func(i)"></view></view>',
'<view><block wx:for="{{5}}" wx:for-item="i" wx:for-index="__i0__" wx:key="*this"><view data-event-opts="{{[[\'tap\',[[\'func\',[\'$0\'],[[[5,\'\',__i0__]]]]]]]}}" class="item" bindtap="__e"></view></block></view>'
......
......@@ -320,17 +320,19 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
}
}
const testCatch = function (path) {
// TODO 仅使用 name 容易误判
if (path.node.object.name === '$event' && path.node.property.name ===
'stopPropagation') {
isCatch = true
path.stop()
}
}
// 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理
if (anonymous && isSafeScoped(state)) {
funcPath.traverse({
noScope: true,
MemberExpression (path) {
if (path.node.object.name === '$event' && path.node.property.name ===
'stopPropagation') {
isCatch = true
path.stop()
}
},
MemberExpression: testCatch,
AssignmentExpression (path) { // "update:title": function($event) {title = $event}
const left = path.node.left
const right = path.node.right
......@@ -368,6 +370,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
if (anonymous) {
// 处理复杂表达式中使用的局部变量(主要在v-for中定义)
funcPath.traverse({
MemberExpression: testCatch,
Identifier (path) {
const scope = path.scope
const node = path.node
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册