Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yangkaifeng
uni-app
提交
1f09a4ae
U
uni-app
项目概览
yangkaifeng
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1f09a4ae
编写于
8月 26, 2019
作者:
fxy060608
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mp): events with multiple statements #720
上级
221f22ff
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
56 addition
and
33 deletion
+56
-33
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
...es/uni-template-compiler/__tests__/compiler-extra.spec.js
+5
-1
packages/uni-template-compiler/__tests__/demo.js
packages/uni-template-compiler/__tests__/demo.js
+1
-1
packages/uni-template-compiler/lib/script/traverse/data/event.js
...s/uni-template-compiler/lib/script/traverse/data/event.js
+50
-31
未找到文件。
packages/uni-template-compiler/__tests__/compiler-extra.spec.js
浏览文件 @
1f09a4ae
...
...
@@ -252,6 +252,10 @@ describe('mp:compiler-extra', () => {
})
it
(
'
generate events inside v-for
'
,
()
=>
{
assertCodegen
(
`<view v-for="item in dataList" :key="item.id" @click="click1(item, 1);click2(item, 2);"/>`
,
`<block wx:for="{{dataList}}" wx:for-item="item" wx:for-index="__i0__" wx:key="id"><view data-event-opts="{{[['tap',[['click1',['$0',1],[[['dataList','id',item.id]]]],['click2',['$0',2],[[['dataList','id',item.id]]]]]]]}}" bindtap="__e"></view></block>`
)
// TODO vue的数字 item 是从1,小程序是从0,后续考虑抹平差异
assertCodegen
(
`<view>1<view v-for="item in items" :key="item"><input v-for="item1 in item" :key="item1" @input="handle" @click="e=>count++"></view></view>`
,
...
...
packages/uni-template-compiler/__tests__/demo.js
浏览文件 @
1f09a4ae
...
...
@@ -2,7 +2,7 @@ const compiler = require('../lib')
const
res
=
compiler
.
compile
(
`
<view
@touchmove="a.touchmove">{{t.a}}{{t['a']}}{{t.a(b)}}{{t['a'](b)}}{{u.t.a(b)}}{{u.t.a}}</view
>
<view
v-for="item in dataList" :key="item.id" @click="click1(item, 1);click2(item, 2);"/
>
`
,
{
resourcePath
:
'
/User/fxy/Documents/test.wxml
'
,
mp
:
{
...
...
packages/uni-template-compiler/lib/script/traverse/data/event.js
浏览文件 @
1f09a4ae
...
...
@@ -167,6 +167,39 @@ function getMethodName (methodName) {
return
methodName
===
'
__HOLDER__
'
?
''
:
methodName
}
function
parseEventByCallExpression
(
callExpr
,
methods
)
{
let
methodName
=
callExpr
.
callee
.
name
if
(
methodName
===
'
$set
'
)
{
methodName
=
INTERNAL_SET_SYNC
}
const
arrayExpression
=
[
t
.
stringLiteral
(
getMethodName
(
methodName
))]
const
args
=
callExpr
.
arguments
if
(
methodName
===
INTERNAL_SET_SYNC
)
{
// v-bind:title.sync="doc.title"
// ['$set',['doc.a','title','$event']]
const
argsExpression
=
[]
argsExpression
.
push
(
t
.
memberExpression
(
args
[
0
],
t
.
identifier
(
args
[
1
].
value
))
)
argsExpression
.
push
(
t
.
stringLiteral
(
args
[
1
].
value
))
argsExpression
.
push
(
t
.
stringLiteral
(
'
$event
'
))
arrayExpression
.
push
(
t
.
arrayExpression
(
argsExpression
))
}
else
{
if
(
args
.
length
)
{
const
argsExpression
=
[]
args
.
forEach
(
arg
=>
{
if
(
t
.
isIdentifier
(
arg
)
&&
arg
.
name
===
'
$event
'
)
{
argsExpression
.
push
(
t
.
stringLiteral
(
'
$event
'
))
}
else
{
argsExpression
.
push
(
arg
)
}
})
arrayExpression
.
push
(
t
.
arrayExpression
(
argsExpression
))
}
}
methods
.
push
(
t
.
arrayExpression
(
arrayExpression
))
}
function
parseEvent
(
keyPath
,
valuePath
,
state
,
isComponent
,
isNativeOn
=
false
,
tagName
,
ret
)
{
const
key
=
keyPath
.
node
let
type
=
key
.
value
||
key
.
name
...
...
@@ -235,7 +268,22 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
methods
.
push
(
addEventExpressionStatement
(
funcPath
,
state
,
isCustom
))
}
else
{
let
anonymous
=
true
funcPath
.
traverse
({
// "click":function($event) {click1(item);click2(item);}
const
body
=
funcPath
.
node
.
body
.
body
if
(
body
.
length
)
{
const
exprStatements
=
body
.
filter
(
node
=>
{
return
t
.
isExpressionStatement
(
node
)
&&
t
.
isCallExpression
(
node
.
expression
)
})
if
(
exprStatements
.
length
===
body
.
length
)
{
anonymous
=
false
exprStatements
.
forEach
(
exprStatement
=>
{
parseEventByCallExpression
(
exprStatement
.
expression
,
methods
)
})
}
}
anonymous
&&
funcPath
.
traverse
({
noScope
:
true
,
MemberExpression
(
path
)
{
if
(
path
.
node
.
object
.
name
===
'
$event
'
&&
path
.
node
.
property
.
name
===
...
...
@@ -271,36 +319,7 @@ function parseEvent (keyPath, valuePath, state, isComponent, isNativeOn = false,
if
(
t
.
isCallExpression
(
argument
))
{
if
(
t
.
isIdentifier
(
argument
.
callee
))
{
anonymous
=
false
let
methodName
=
argument
.
callee
.
name
if
(
methodName
===
'
$set
'
)
{
methodName
=
INTERNAL_SET_SYNC
}
const
arrayExpression
=
[
t
.
stringLiteral
(
getMethodName
(
methodName
))]
const
args
=
argument
.
arguments
if
(
methodName
===
INTERNAL_SET_SYNC
)
{
// v-bind:title.sync="doc.title"
// ['$set',['doc.a','title','$event']]
const
argsExpression
=
[]
argsExpression
.
push
(
t
.
memberExpression
(
args
[
0
],
t
.
identifier
(
args
[
1
].
value
))
)
argsExpression
.
push
(
t
.
stringLiteral
(
args
[
1
].
value
))
argsExpression
.
push
(
t
.
stringLiteral
(
'
$event
'
))
arrayExpression
.
push
(
t
.
arrayExpression
(
argsExpression
))
}
else
{
if
(
args
.
length
)
{
const
argsExpression
=
[]
args
.
forEach
(
arg
=>
{
if
(
t
.
isIdentifier
(
arg
)
&&
arg
.
name
===
'
$event
'
)
{
argsExpression
.
push
(
t
.
stringLiteral
(
'
$event
'
))
}
else
{
argsExpression
.
push
(
arg
)
}
})
arrayExpression
.
push
(
t
.
arrayExpression
(
argsExpression
))
}
}
methods
.
push
(
t
.
arrayExpression
(
arrayExpression
))
parseEventByCallExpression
(
argument
,
methods
)
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录