fix(script-setup): ensure useContext() return valid context
This commit is contained in:
parent
a764814b8e
commit
73cdb9d420
@ -1,4 +1,8 @@
|
|||||||
import { getCurrentInstance, SetupContext } from './component'
|
import {
|
||||||
|
getCurrentInstance,
|
||||||
|
SetupContext,
|
||||||
|
createSetupContext
|
||||||
|
} from './component'
|
||||||
import { EmitFn, EmitsOptions } from './componentEmits'
|
import { EmitFn, EmitsOptions } from './componentEmits'
|
||||||
import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
|
import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
|
||||||
import { warn } from './warning'
|
import { warn } from './warning'
|
||||||
@ -53,5 +57,9 @@ export function defineEmit(emitOptions?: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useContext(): SetupContext {
|
export function useContext(): SetupContext {
|
||||||
return getCurrentInstance()!.setupContext!
|
const i = getCurrentInstance()!
|
||||||
|
if (__DEV__ && !i) {
|
||||||
|
warn(`useContext() called without active instance.`)
|
||||||
|
}
|
||||||
|
return i.setupContext || (i.setupContext = createSetupContext(i))
|
||||||
}
|
}
|
||||||
|
@ -746,7 +746,9 @@ const attrHandlers: ProxyHandler<Data> = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSetupContext(instance: ComponentInternalInstance): SetupContext {
|
export function createSetupContext(
|
||||||
|
instance: ComponentInternalInstance
|
||||||
|
): SetupContext {
|
||||||
const expose: SetupContext['expose'] = exposed => {
|
const expose: SetupContext['expose'] = exposed => {
|
||||||
if (__DEV__ && instance.exposed) {
|
if (__DEV__ && instance.exposed) {
|
||||||
warn(`expose() should be called only once per setup().`)
|
warn(`expose() should be called only once per setup().`)
|
||||||
|
Loading…
Reference in New Issue
Block a user