|
1 | 1 | // @flow
|
2 | 2 |
|
3 |
| -import Vue from 'vue' |
4 | 3 | import { compileToFunctions } from 'vue-template-compiler'
|
5 | 4 |
|
6 |
| -function startsWithTag (str: SlotValue): boolean { |
7 |
| - return typeof str === 'string' && str.trim()[0] === '<' |
8 |
| -} |
9 |
| - |
10 | 5 | function createVNodesForSlot (
|
11 | 6 | h: Function,
|
12 | 7 | slotValue: SlotValue,
|
13 | 8 | name: string,
|
14 |
| - _Vue: any |
| 9 | + vm: Component |
15 | 10 | ): 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 |
24 | 12 | 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 } |
38 | 26 | }
|
39 |
| - |
40 |
| - vnode.data.slot = name |
41 | 27 | return vnode
|
42 | 28 | }
|
43 | 29 |
|
44 | 30 | export function createSlotVNodes (
|
45 | 31 | h: Function,
|
46 | 32 | slots: SlotsObject,
|
47 |
| - _Vue: any |
| 33 | + vm: Component |
48 | 34 | ): Array<VNode | string> {
|
49 | 35 | return Object.keys(slots).reduce((acc, key) => {
|
50 | 36 | const content = slots[key]
|
51 | 37 | if (Array.isArray(content)) {
|
52 | 38 | const nodes = content.map(
|
53 |
| - slotDef => createVNodesForSlot(h, slotDef, key, _Vue) |
| 39 | + slotDef => createVNodesForSlot(h, slotDef, key, vm) |
54 | 40 | )
|
55 | 41 | return acc.concat(nodes)
|
56 | 42 | }
|
57 | 43 |
|
58 |
| - return acc.concat(createVNodesForSlot(h, content, key, _Vue)) |
| 44 | + return acc.concat(createVNodesForSlot(h, content, key, vm)) |
59 | 45 | }, [])
|
60 | 46 | }
|
0 commit comments