Skip to content

Commit d4c118b

Browse files
authored
fix: support registered components in scopedSlots (#1065)
1 parent c69896a commit d4c118b

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import createScopedSlots from './create-scoped-slots'
77

88
export default function createFunctionalComponent (
99
component: Component,
10-
mountingOptions: Options
10+
mountingOptions: Options,
11+
_Vue: Component
1112
): Component {
1213
if (mountingOptions.context && typeof mountingOptions.context !== 'object') {
1314
throwError('mount.context must be an object')
@@ -29,7 +30,7 @@ export default function createFunctionalComponent (
2930
})
3031
}
3132

32-
context.scopedSlots = createScopedSlots(mountingOptions.scopedSlots)
33+
context.scopedSlots = createScopedSlots(mountingOptions.scopedSlots, _Vue)
3334

3435
return {
3536
render (h: Function) {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default function createInstance (
7171
(component.options && component.options.functional) ||
7272
component.functional
7373
) {
74-
component = createFunctionalComponent(component, options)
74+
component = createFunctionalComponent(component, options, _Vue)
7575
} else if (options.context) {
7676
throwError(
7777
`mount.context can only be used when mounting a ` +
@@ -116,7 +116,7 @@ export default function createInstance (
116116
options.provide = () => obj
117117
}
118118

119-
const scopedSlots = createScopedSlots(options.scopedSlots)
119+
const scopedSlots = createScopedSlots(options.scopedSlots, _Vue)
120120

121121
if (options.parentComponent && !isPlainObject(options.parentComponent)) {
122122
throwError(

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// @flow
22

3-
import Vue from 'vue'
43
import { compileToFunctions } from 'vue-template-compiler'
54
import { throwError, vueVersion } from 'shared/util'
65

76
function isDestructuringSlotScope (slotScope: string): boolean {
87
return slotScope[0] === '{' && slotScope[slotScope.length - 1] === '}'
98
}
109

11-
function getVueTemplateCompilerHelpers (): { [name: string]: Function } {
12-
const vue = new Vue()
10+
function getVueTemplateCompilerHelpers (
11+
_Vue: Component
12+
): { [name: string]: Function } {
13+
// $FlowIgnore
14+
const vue = new _Vue()
1315
const helpers = {}
1416
const names = [
1517
'_c',
@@ -52,7 +54,8 @@ function customWarn (msg) {
5254
}
5355

5456
export default function createScopedSlots (
55-
scopedSlotsOption: ?{ [slotName: string]: string | Function }
57+
scopedSlotsOption: ?{ [slotName: string]: string | Function },
58+
_Vue: Component
5659
): {
5760
[slotName: string]: (props: Object) => VNode | Array<VNode>
5861
} {
@@ -61,7 +64,7 @@ export default function createScopedSlots (
6164
return scopedSlots
6265
}
6366
validateEnvironment()
64-
const helpers = getVueTemplateCompilerHelpers()
67+
const helpers = getVueTemplateCompilerHelpers(_Vue)
6568
for (const scopedSlotName in scopedSlotsOption) {
6669
const slot = scopedSlotsOption[scopedSlotName]
6770
const isFn = typeof slot === 'function'

Diff for: test/specs/mounting-options/scopedSlots.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
describeWithShallowAndMount,
33
vueVersion
44
} from '~resources/utils'
5+
import { createLocalVue } from '~vue/test-utils'
56
import ComponentWithScopedSlots from '~resources/components/component-with-scoped-slots.vue'
67
import { itDoNotRunIf } from 'conditional-specs'
78

@@ -234,4 +235,28 @@ describeWithShallowAndMount('scopedSlots', mountingMethod => {
234235
.with.property('message', message)
235236
}
236237
)
238+
239+
itDoNotRunIf(
240+
vueVersion < 2.5 || mountingMethod.name !== 'mount',
241+
'renders using localVue constructor',
242+
() => {
243+
const RegisteredComponent = {
244+
render: h => h('span')
245+
}
246+
const TestComponent = {
247+
template: '<div><slot name="single" /></div>'
248+
}
249+
250+
const localVue = createLocalVue()
251+
localVue.component('registered-component', RegisteredComponent)
252+
253+
const wrapper = mountingMethod(TestComponent, {
254+
scopedSlots: {
255+
single: '<template><registered-component /></template>'
256+
},
257+
localVue
258+
})
259+
260+
expect(wrapper.html()).to.contain('span')
261+
})
237262
})

0 commit comments

Comments
 (0)