mirror of https://github.com/vuejs/core.git
dx(runtime-core): warn when expose() is misused (#7221)
This commit is contained in:
parent
13dc28aeff
commit
4902354925
|
@ -225,4 +225,43 @@ describe('api: expose', () => {
|
||||||
expect(grandChildRef.value.$parent).toBe(childRef.value)
|
expect(grandChildRef.value.$parent).toBe(childRef.value)
|
||||||
expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root)
|
expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('warning for ref', () => {
|
||||||
|
const Comp = defineComponent({
|
||||||
|
setup(_, { expose }) {
|
||||||
|
expose(ref(1))
|
||||||
|
return () => null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
render(h(Comp), nodeOps.createElement('div'))
|
||||||
|
expect(
|
||||||
|
'expose() should be passed a plain object, received ref'
|
||||||
|
).toHaveBeenWarned()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('warning for array', () => {
|
||||||
|
const Comp = defineComponent({
|
||||||
|
setup(_, { expose }) {
|
||||||
|
expose(['focus'])
|
||||||
|
return () => null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
render(h(Comp), nodeOps.createElement('div'))
|
||||||
|
expect(
|
||||||
|
'expose() should be passed a plain object, received array'
|
||||||
|
).toHaveBeenWarned()
|
||||||
|
})
|
||||||
|
|
||||||
|
test('warning for function', () => {
|
||||||
|
const Comp = defineComponent({
|
||||||
|
setup(_, { expose }) {
|
||||||
|
expose(() => null)
|
||||||
|
return () => null
|
||||||
|
}
|
||||||
|
})
|
||||||
|
render(h(Comp), nodeOps.createElement('div'))
|
||||||
|
expect(
|
||||||
|
'expose() should be passed a plain object, received function'
|
||||||
|
).toHaveBeenWarned()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { VNode, VNodeChild, isVNode } from './vnode'
|
import { VNode, VNodeChild, isVNode } from './vnode'
|
||||||
import {
|
import {
|
||||||
|
isRef,
|
||||||
pauseTracking,
|
pauseTracking,
|
||||||
resetTracking,
|
resetTracking,
|
||||||
shallowReadonly,
|
shallowReadonly,
|
||||||
|
@ -47,6 +48,7 @@ import {
|
||||||
} from './componentEmits'
|
} from './componentEmits'
|
||||||
import {
|
import {
|
||||||
EMPTY_OBJ,
|
EMPTY_OBJ,
|
||||||
|
isArray,
|
||||||
isFunction,
|
isFunction,
|
||||||
NOOP,
|
NOOP,
|
||||||
isObject,
|
isObject,
|
||||||
|
@ -913,8 +915,25 @@ export function createSetupContext(
|
||||||
instance: ComponentInternalInstance
|
instance: ComponentInternalInstance
|
||||||
): SetupContext {
|
): SetupContext {
|
||||||
const expose: SetupContext['expose'] = exposed => {
|
const expose: SetupContext['expose'] = exposed => {
|
||||||
if (__DEV__ && instance.exposed) {
|
if (__DEV__) {
|
||||||
warn(`expose() should be called only once per setup().`)
|
if (instance.exposed) {
|
||||||
|
warn(`expose() should be called only once per setup().`)
|
||||||
|
}
|
||||||
|
if (exposed != null) {
|
||||||
|
let exposedType: string = typeof exposed
|
||||||
|
if (exposedType === 'object') {
|
||||||
|
if (isArray(exposed)) {
|
||||||
|
exposedType = 'array'
|
||||||
|
} else if (isRef(exposed)) {
|
||||||
|
exposedType = 'ref'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (exposedType !== 'object') {
|
||||||
|
warn(
|
||||||
|
`expose() should be passed a plain object, received ${exposedType}.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
instance.exposed = exposed || {}
|
instance.exposed = exposed || {}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue