Skip to content

Commit 73cdb9d

Browse files
committedNov 26, 2020
fix(script-setup): ensure useContext() return valid context
1 parent a764814 commit 73cdb9d

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed
 

‎packages/runtime-core/src/apiSetupHelpers.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { getCurrentInstance, SetupContext } from './component'
1+
import {
2+
getCurrentInstance,
3+
SetupContext,
4+
createSetupContext
5+
} from './component'
26
import { EmitFn, EmitsOptions } from './componentEmits'
37
import { ComponentObjectPropsOptions, ExtractPropTypes } from './componentProps'
48
import { warn } from './warning'
@@ -53,5 +57,9 @@ export function defineEmit(emitOptions?: any) {
5357
}
5458

5559
export function useContext(): SetupContext {
56-
return getCurrentInstance()!.setupContext!
60+
const i = getCurrentInstance()!
61+
if (__DEV__ && !i) {
62+
warn(`useContext() called without active instance.`)
63+
}
64+
return i.setupContext || (i.setupContext = createSetupContext(i))
5765
}

‎packages/runtime-core/src/component.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,9 @@ const attrHandlers: ProxyHandler<Data> = {
746746
}
747747
}
748748

749-
function createSetupContext(instance: ComponentInternalInstance): SetupContext {
749+
export function createSetupContext(
750+
instance: ComponentInternalInstance
751+
): SetupContext {
750752
const expose: SetupContext['expose'] = exposed => {
751753
if (__DEV__ && instance.exposed) {
752754
warn(`expose() should be called only once per setup().`)

0 commit comments

Comments
 (0)
Please sign in to comment.