Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MuGuiLin
uni-app
提交
1b730d2b
U
uni-app
项目概览
MuGuiLin
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
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,发现更多精彩内容 >>
提交
1b730d2b
编写于
6月 27, 2022
作者:
Q
qiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mp): slot name dynamic data (question/82506)
上级
51940f69
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
46 addition
and
19 deletion
+46
-19
packages/uni-template-compiler/__tests__/compiler.spec.js
packages/uni-template-compiler/__tests__/compiler.spec.js
+24
-0
packages/uni-template-compiler/lib/module.js
packages/uni-template-compiler/lib/module.js
+6
-5
packages/uni-template-compiler/lib/script/traverse/render-slot.js
.../uni-template-compiler/lib/script/traverse/render-slot.js
+1
-1
packages/uni-template-compiler/lib/template/traverse.js
packages/uni-template-compiler/lib/template/traverse.js
+15
-13
未找到文件。
packages/uni-template-compiler/__tests__/compiler.spec.js
浏览文件 @
1b730d2b
...
...
@@ -182,6 +182,30 @@ describe('mp:compiler', () => {
'
<view><slot name="one"></slot></view>
'
,
'
<view><slot name="one"></slot></view>
'
)
assertCodegen
(
'
<view><slot :name="one"></slot></view>
'
,
'
<view><slot name="{{one}}"></slot></view>
'
)
assertCodegen
(
'
<view><slot :name="one+
\'
test
\'
"></slot></view>
'
,
'
<view><slot name="{{one+
\'
test
\'
}}"></slot></view>
'
)
assertCodegen
(
'
<view><slot :name="one" :test="test"></slot></view>
'
,
'
<view><slot name="{{one}}"></slot></view>
'
,
'
with(this){{$setScopedSlotsParams(one,{"test":test})}}
'
,
{
scopedSlotsCompiler
:
'
augmented
'
}
)
assertCodegen
(
'
<view><slot :name="one">text</slot></view>
'
,
'
<view><block wx:if="{{$slots[one]}}"><slot name="{{one}}"></slot></block><block wx:else>text</block></view>
'
)
assertCodegen
(
'
<view><slot :name="one+
\'
test
\'
">text</slot></view>
'
,
'
<view><block wx:if="{{$slots[one+
\'
test
\'
]}}"><slot name="{{one+
\'
test
\'
}}"></slot></block><block wx:else>text</block></view>
'
)
})
// it('generate slot fallback content', () => {
...
...
packages/uni-template-compiler/lib/module.js
浏览文件 @
1b730d2b
...
...
@@ -15,12 +15,13 @@ module.exports = {
preTransformNode
(
el
,
{
warn
})
{
if
(
el
.
tag
===
'
slot
'
&&
!
el
.
attrsMap
.
name
)
{
const
attrsMap
=
el
.
attrsMap
if
(
el
.
tag
===
'
slot
'
&&
!
(
attrsMap
.
name
||
attrsMap
[
'
:name
'
]))
{
el
.
attrsList
.
push
({
name
:
'
SLOT_DEFAULT
'
,
value
:
true
})
el
.
attrsMap
.
SLOT_DEFAULT
=
true
attrsMap
.
SLOT_DEFAULT
=
true
}
// 处理 attr
el
.
attrsList
.
forEach
(
attr
=>
{
...
...
@@ -31,11 +32,11 @@ module.exports = {
const
origName
=
attr
.
name
const
newName
=
origName
.
replace
(
'
.lazy
'
,
''
)
attr
.
name
=
newName
el
.
attrsMap
[
newName
]
=
attr
.
value
delete
el
.
attrsMap
[
origName
]
attrsMap
[
newName
]
=
attr
.
value
delete
attrsMap
[
origName
]
}
else
if
(
onRE
.
test
(
attr
.
name
)
&&
!
attr
.
value
.
trim
())
{
// 事件为空
attr
.
value
=
'
__HOLDER__
'
el
.
attrsMap
[
attr
.
name
]
=
attr
.
value
attrsMap
[
attr
.
name
]
=
attr
.
value
}
})
// 暂不支持的指令
...
...
packages/uni-template-compiler/lib/script/traverse/render-slot.js
浏览文件 @
1b730d2b
...
...
@@ -32,7 +32,7 @@ module.exports = function getRenderSlot (path, state) {
const
scoped
=
state
.
scoped
// TODO 判断是否包含作用域内变量
const
renderSlotStatementArray
=
scoped
&&
scoped
.
length
?
scoped
[
scoped
.
length
-
1
].
renderSlotStatementArray
:
state
.
renderSlotStatementArray
renderSlotStatementArray
.
push
(
t
.
expressionStatement
(
t
.
callExpression
(
t
.
identifier
(
'
$setScopedSlotsParams
'
),
[
t
.
stringLiteral
(
name
.
node
.
value
)
,
valueNode
])))
renderSlotStatementArray
.
push
(
t
.
expressionStatement
(
t
.
callExpression
(
t
.
identifier
(
'
$setScopedSlotsParams
'
),
[
name
.
node
,
valueNode
])))
}
// TODO 组件嵌套
}
packages/uni-template-compiler/lib/template/traverse.js
浏览文件 @
1b730d2b
...
...
@@ -270,7 +270,7 @@ function traverseArrayExpression (arrayExprNodes, state) {
},
[])
}
function
genSlotNode
(
slotName
,
slotNode
,
fallbackNodes
,
state
)
{
function
genSlotNode
(
slotName
,
slotNode
,
fallbackNodes
,
state
,
isStaticSlotName
=
true
)
{
if
(
!
fallbackNodes
||
t
.
isNullLiteral
(
fallbackNodes
))
{
return
slotNode
}
...
...
@@ -282,7 +282,7 @@ function genSlotNode (slotName, slotNode, fallbackNodes, state) {
return
[{
type
:
'
block
'
,
attr
:
{
[
prefix
+
'
if
'
]:
'
{{$slots.
'
+
slotName
+
'
}}
'
[
prefix
+
'
if
'
]:
isStaticSlotName
?
'
{{$slots.
'
+
slotName
+
'
}}
'
:
'
{{$slots[
'
+
slotName
.
replace
(
/^{{/
,
''
).
replace
(
/}}$/
,
''
)
+
'
]
}}
'
},
children
:
[].
concat
(
slotNode
)
},
{
...
...
@@ -297,12 +297,9 @@ function genSlotNode (slotName, slotNode, fallbackNodes, state) {
}
function
traverseRenderSlot
(
callExprNode
,
state
)
{
if
(
!
t
.
isStringLiteral
(
callExprNode
.
arguments
[
0
]))
{
state
.
errors
.
add
(
uniI18n
.
__
(
'
templateCompiler.notSupportDynamicSlotName
'
,
{
0
:
'
v-slot
'
}))
return
}
const
slotName
=
callExprNode
.
arguments
[
0
].
value
const
slotNameNode
=
callExprNode
.
arguments
[
0
]
const
isStaticSlotName
=
t
.
isStringLiteral
(
slotNameNode
)
const
slotName
=
isStaticSlotName
?
slotNameNode
.
value
:
genCode
(
slotNameNode
)
let
deleteSlotName
=
false
// 标记是否组件 slot 手动指定了 name="default"
if
(
state
.
options
.
scopedSlotsCompiler
!==
'
augmented
'
&&
callExprNode
.
arguments
.
length
>
2
)
{
// 作用域插槽
...
...
@@ -312,6 +309,10 @@ function traverseRenderSlot (callExprNode, state) {
})
deleteSlotName
=
props
.
SLOT_DEFAULT
&&
Object
.
keys
(
props
).
length
===
1
if
(
!
deleteSlotName
)
{
if
(
!
isStaticSlotName
)
{
state
.
errors
.
add
(
uniI18n
.
__
(
'
templateCompiler.notSupportDynamicSlotName
'
,
{
0
:
'
v-slot
'
}))
return
}
delete
props
.
SLOT_DEFAULT
return
genSlotNode
(
slotName
,
...
...
@@ -334,10 +335,11 @@ function traverseRenderSlot (callExprNode, state) {
delete
node
.
attr
.
name
}
return
genSlotNode
(
slotName
,
node
,
callExprNode
.
arguments
[
1
],
state
)
return
genSlotNode
(
slotName
,
node
,
callExprNode
.
arguments
[
1
],
state
,
isStaticSlotName
)
}
function
traverseResolveScopedSlots
(
callExprNode
,
state
)
{
const
options
=
state
.
options
function
single
(
children
,
slotName
,
ignore
)
{
if
(
Array
.
isArray
(
children
)
&&
children
.
length
===
1
)
{
const
child
=
children
[
0
]
...
...
@@ -371,14 +373,14 @@ function traverseResolveScopedSlots (callExprNode, state) {
const
slotName
=
keyProperty
.
value
.
value
const
returnExprNodes
=
fnProperty
.
value
.
body
.
body
[
0
].
argument
const
parentNode
=
callExprNode
.
$node
if
(
slotNode
.
scopedSlotsCompiler
!==
'
augmented
'
&&
!
proxyProperty
)
{
const
resourcePath
=
state
.
options
.
resourcePath
if
(
options
.
scopedSlotsCompiler
!==
'
augmented
'
&&
slotNode
.
scopedSlotsCompiler
!==
'
augmented
'
&&
!
proxyProperty
)
{
const
resourcePath
=
options
.
resourcePath
const
ownerName
=
path
.
basename
(
resourcePath
,
path
.
extname
(
resourcePath
))
const
parentName
=
parentNode
.
type
const
paramExprNode
=
fnProperty
.
value
.
params
[
0
]
return
state
.
options
.
platform
.
resolveScopedSlots
(
return
options
.
platform
.
resolveScopedSlots
(
slotName
,
{
genCode
,
generate
,
...
...
@@ -398,7 +400,7 @@ function traverseResolveScopedSlots (callExprNode, state) {
state
)
}
if
(
state
.
options
.
scopedSlotsCompiler
===
'
auto
'
&&
slotNode
.
scopedSlotsCompiler
===
'
augmented
'
)
{
if
(
options
.
scopedSlotsCompiler
===
'
auto
'
&&
slotNode
.
scopedSlotsCompiler
===
'
augmented
'
)
{
parentNode
.
attr
[
'
scoped-slots-compiler
'
]
=
'
augmented
'
}
const
children
=
normalizeChildren
(
traverseExpr
(
returnExprNodes
,
state
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录