Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-app
提交
825aa771
U
uni-app
项目概览
DCloud
/
uni-app
29 天 前同步成功
通知
702
Star
38704
Fork
3642
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
7
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
7
Issue
7
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
825aa771
编写于
2月 11, 2022
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mp): 修复 v-for 遍历复杂表达式时 stop 修饰符无效的问题
上级
f1861737
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
24 addition
and
7 deletion
+24
-7
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
...es/uni-template-compiler/__tests__/compiler-extra.spec.js
+14
-0
packages/uni-template-compiler/lib/script/traverse/data/event.js
...s/uni-template-compiler/lib/script/traverse/data/event.js
+10
-7
未找到文件。
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
浏览文件 @
825aa771
...
@@ -346,6 +346,20 @@ describe('mp:compiler-extra', () => {
...
@@ -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><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>
'
'
<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
(
assertCodegen
(
'
<view><view class="item" v-for="i in 5" :key="i" @click="func(i)"></view></view>
'
,
'
<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>
'
'
<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>
'
...
...
packages/uni-template-compiler/lib/script/traverse/data/event.js
浏览文件 @
825aa771
...
@@ -320,17 +320,19 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
...
@@ -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 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理
// 如果 v-for 遍历的值为 数组、对象、方法 则进入底部匿名表达式处理
if
(
anonymous
&&
isSafeScoped
(
state
))
{
if
(
anonymous
&&
isSafeScoped
(
state
))
{
funcPath
.
traverse
({
funcPath
.
traverse
({
noScope
:
true
,
noScope
:
true
,
MemberExpression
(
path
)
{
MemberExpression
:
testCatch
,
if
(
path
.
node
.
object
.
name
===
'
$event
'
&&
path
.
node
.
property
.
name
===
'
stopPropagation
'
)
{
isCatch
=
true
path
.
stop
()
}
},
AssignmentExpression
(
path
)
{
// "update:title": function($event) {title = $event}
AssignmentExpression
(
path
)
{
// "update:title": function($event) {title = $event}
const
left
=
path
.
node
.
left
const
left
=
path
.
node
.
left
const
right
=
path
.
node
.
right
const
right
=
path
.
node
.
right
...
@@ -368,6 +370,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
...
@@ -368,6 +370,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
if
(
anonymous
)
{
if
(
anonymous
)
{
// 处理复杂表达式中使用的局部变量(主要在v-for中定义)
// 处理复杂表达式中使用的局部变量(主要在v-for中定义)
funcPath
.
traverse
({
funcPath
.
traverse
({
MemberExpression
:
testCatch
,
Identifier
(
path
)
{
Identifier
(
path
)
{
const
scope
=
path
.
scope
const
scope
=
path
.
scope
const
node
=
path
.
node
const
node
=
path
.
node
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录