Skip to content

Commit 1540159

Browse files
committed
improve createSlotVNodes
1 parent 970eac9 commit 1540159

File tree

3 files changed

+21
-36
lines changed

3 files changed

+21
-36
lines changed

Diff for: packages/create-instance/create-functional-component.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { createSlotVNodes } from './create-slot-vnodes'
77
export default function createFunctionalComponent (
88
component: Component,
99
mountingOptions: Options,
10-
_Vue: Component
1110
): Component {
1211
if (mountingOptions.context && typeof mountingOptions.context !== 'object') {
1312
throwError('mount.context must be an object')
@@ -26,7 +25,7 @@ export default function createFunctionalComponent (
2625
mountingOptions.context.children.map(
2726
x => (typeof x === 'function' ? x(h) : x)
2827
)) ||
29-
createSlotVNodes(h, mountingOptions.slots || {}, _Vue)
28+
createSlotVNodes(h, mountingOptions.slots || {}, this)
3029
)
3130
},
3231
name: component.name,

Diff for: packages/create-instance/create-instance.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function createInstance (
5151
(component.options && component.options.functional) ||
5252
component.functional
5353
) {
54-
component = createFunctionalComponent(component, options, _Vue)
54+
component = createFunctionalComponent(component, options)
5555
} else if (options.context) {
5656
throwError(
5757
`mount.context can only be used when mounting a ` + `functional component`
@@ -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, _Vue)
145+
? createSlotVNodes(h, options.slots, this)
146146
: undefined
147147
return h(
148148
Constructor,

Diff for: packages/create-instance/create-slot-vnodes.js

+18-32
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,46 @@
11
// @flow
22

3-
import Vue from 'vue'
43
import { compileToFunctions } from 'vue-template-compiler'
54

6-
function startsWithTag (str: SlotValue): boolean {
7-
return typeof str === 'string' && str.trim()[0] === '<'
8-
}
9-
105
function createVNodesForSlot (
116
h: Function,
127
slotValue: SlotValue,
138
name: string,
14-
_Vue: any
9+
vm: Component
1510
): VNode | string {
16-
if (typeof slotValue === 'string' && !startsWithTag(slotValue)) {
17-
return slotValue
18-
}
19-
20-
const el =
21-
typeof slotValue === 'string' ? compileToFunctions(slotValue) : slotValue
22-
23-
let vnode = h(el)
11+
let vnode
2412
if (typeof slotValue === 'string') {
25-
const vue = new Vue()
26-
const _vue = new _Vue()
27-
for (const key in _vue._renderProxy) {
28-
if (!(vue._renderProxy[key])) {
29-
vue._renderProxy[key] = _vue._renderProxy[key]
30-
}
31-
}
32-
try {
33-
// $FlowIgnore
34-
vnode = el.render.call(vue._renderProxy, h)
35-
vnode = h(vnode.tag, vnode.data || {}, vnode.children)
36-
} catch (e) {
37-
}
13+
const el = compileToFunctions(`<div>${slotValue}{{ }}</div>`)
14+
const _staticRenderFns = vm._renderProxy.$options.staticRenderFns
15+
vm._renderProxy.$options.staticRenderFns = el.staticRenderFns
16+
vnode = el.render.call(vm._renderProxy, h)
17+
vm._renderProxy.$options.staticRenderFns = _staticRenderFns
18+
vnode = vnode.children[0]
19+
} else {
20+
vnode = h(slotValue)
21+
}
22+
if (vnode.data) {
23+
vnode.data.slot = name
24+
} else {
25+
vnode.data = { slot: name }
3826
}
39-
40-
vnode.data.slot = name
4127
return vnode
4228
}
4329

4430
export function createSlotVNodes (
4531
h: Function,
4632
slots: SlotsObject,
47-
_Vue: any
33+
vm: Component
4834
): Array<VNode | string> {
4935
return Object.keys(slots).reduce((acc, key) => {
5036
const content = slots[key]
5137
if (Array.isArray(content)) {
5238
const nodes = content.map(
53-
slotDef => createVNodesForSlot(h, slotDef, key, _Vue)
39+
slotDef => createVNodesForSlot(h, slotDef, key, vm)
5440
)
5541
return acc.concat(nodes)
5642
}
5743

58-
return acc.concat(createVNodesForSlot(h, content, key, _Vue))
44+
return acc.concat(createVNodesForSlot(h, content, key, vm))
5945
}, [])
6046
}

0 commit comments

Comments
 (0)