Skip to content

Commit a0d124c

Browse files
committed
add createVNodes()
1 parent 388e2d5 commit a0d124c

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

packages/create-instance/create-functional-component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function createFunctionalComponent (
2525
mountingOptions.context.children.map(
2626
x => (typeof x === 'function' ? x(h) : x)
2727
)) ||
28-
createSlotVNodes(h, mountingOptions.slots || {}, this)
28+
createSlotVNodes(this, mountingOptions.slots || {})
2929
)
3030
},
3131
name: component.name,

packages/create-instance/create-instance.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export default function createInstance (
142142
provide: options.provide,
143143
render (h) {
144144
const slots = options.slots
145-
? createSlotVNodes(h, options.slots, this)
145+
? createSlotVNodes(this, options.slots)
146146
: undefined
147147
return h(
148148
Constructor,

packages/create-instance/create-slot-vnodes.js

+24-18
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@
22

33
import { compileToFunctions } from 'vue-template-compiler'
44

5+
function createVNodes (
6+
vm: Component,
7+
slotValue: string
8+
): Array<VNode> {
9+
const el = compileToFunctions(`<div>${slotValue}</div>`)
10+
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
11+
// version < 2.5
12+
if (!vm._renderProxy._staticTrees) {
13+
vm._renderProxy._staticTrees = []
14+
}
15+
vm._renderProxy.$options.staticRenderFns = el.staticRenderFns
16+
const vnode = el.render.call(vm._renderProxy, vm.$createElement)
17+
vm._renderProxy.$options.staticRenderFns = _staticRenderFns
18+
return vnode.children
19+
}
20+
521
function createVNodesForSlot (
6-
h: Function,
22+
vm: Component,
723
slotValue: SlotValue,
824
name: string,
9-
vm: Component
1025
): VNode | string {
1126
let vnode
1227
if (typeof slotValue === 'string') {
13-
const el = compileToFunctions(`<div>${slotValue}</div>`)
14-
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
15-
// version < 2.5
16-
if (!vm._renderProxy._staticTrees) {
17-
vm._renderProxy._staticTrees = []
18-
}
19-
vm._renderProxy.$options.staticRenderFns = el.staticRenderFns
20-
vnode = el.render.call(vm._renderProxy, h)
21-
vm._renderProxy.$options.staticRenderFns = _staticRenderFns
22-
vnode = vnode.children[0]
28+
const vnodes = createVNodes(vm, slotValue)
29+
vnode = vnodes[0]
2330
} else {
24-
vnode = h(slotValue)
31+
vnode = vm.$createElement(slotValue)
2532
}
2633
if (vnode.data) {
2734
vnode.data.slot = name
@@ -32,19 +39,18 @@ function createVNodesForSlot (
3239
}
3340

3441
export function createSlotVNodes (
35-
h: Function,
36-
slots: SlotsObject,
37-
vm: Component
42+
vm: Component,
43+
slots: SlotsObject
3844
): Array<VNode | string> {
3945
return Object.keys(slots).reduce((acc, key) => {
4046
const content = slots[key]
4147
if (Array.isArray(content)) {
4248
const nodes = content.map(
43-
slotDef => createVNodesForSlot(h, slotDef, key, vm)
49+
slotDef => createVNodesForSlot(vm, slotDef, key)
4450
)
4551
return acc.concat(nodes)
4652
}
4753

48-
return acc.concat(createVNodesForSlot(h, content, key, vm))
54+
return acc.concat(createVNodesForSlot(vm, content, key))
4955
}, [])
5056
}

0 commit comments

Comments
 (0)