feat(sfc): useAttrs + useSlots

Deprecate useContext
This commit is contained in:
Evan You 2021-06-22 21:00:26 -04:00
parent 6f6f0cf5dc
commit 63e9e2e9aa
2 changed files with 20 additions and 5 deletions

View File

@ -61,10 +61,27 @@ export function defineEmits() {
*/ */
export const defineEmit = defineEmits export const defineEmit = defineEmits
/**
* @deprecated use `useSlots` and `useAttrs` instead.
*/
export function useContext(): SetupContext { export function useContext(): SetupContext {
if (__DEV__) {
warn(
`\`useContext()\` has been deprecated and will be removed in the ` +
`next minor release. Use \`useSlots()\` and \`useAttrs()\` instead.`
)
}
const i = getCurrentInstance()! const i = getCurrentInstance()!
if (__DEV__ && !i) { if (__DEV__ && !i) {
warn(`useContext() called without active instance.`) warn(`useContext() called without active instance.`)
} }
return i.setupContext || (i.setupContext = createSetupContext(i)) return i.setupContext || (i.setupContext = createSetupContext(i))
} }
export function useSlots(): SetupContext['slots'] {
return useContext().slots
}
export function useAttrs(): SetupContext['attrs'] {
return useContext().attrs
}

View File

@ -815,11 +815,9 @@ export function finishComponentSetup(
} }
} }
const attrHandlers: ProxyHandler<Data> = { const attrDevProxyHandlers: ProxyHandler<Data> = {
get: (target, key: string) => { get: (target, key: string) => {
if (__DEV__) { markAttrsAccessed()
markAttrsAccessed()
}
return target[key] return target[key]
}, },
set: () => { set: () => {
@ -847,7 +845,7 @@ export function createSetupContext(
// properties (overwrites should not be done in prod) // properties (overwrites should not be done in prod)
return Object.freeze({ return Object.freeze({
get attrs() { get attrs() {
return new Proxy(instance.attrs, attrHandlers) return new Proxy(instance.attrs, attrDevProxyHandlers)
}, },
get slots() { get slots() {
return shallowReadonly(instance.slots) return shallowReadonly(instance.slots)