feat(reactivity): expose unref and shallowRef

This commit is contained in:
Evan You
2020-02-22 04:39:32 +01:00
parent 0c67201942
commit e9024bf1b7
6 changed files with 59 additions and 11 deletions

View File

@@ -13,7 +13,8 @@ import {
isRef,
isReactive,
Ref,
ComputedRef
ComputedRef,
unref
} from '@vue/reactivity'
import { warn } from './warning'
import { Slots } from './componentSlots'
@@ -84,8 +85,6 @@ const enum AccessTypes {
OTHER
}
const unwrapRef = (val: unknown) => (isRef(val) ? val.value : val)
export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
get(target: ComponentInternalInstance, key: string) {
// fast path for unscopables when using `with` block
@@ -115,7 +114,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
case AccessTypes.DATA:
return data[key]
case AccessTypes.CONTEXT:
return unwrapRef(renderContext[key])
return unref(renderContext[key])
case AccessTypes.PROPS:
return propsProxy![key]
// default: just fallthrough
@@ -125,7 +124,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
return data[key]
} else if (hasOwn(renderContext, key)) {
accessCache![key] = AccessTypes.CONTEXT
return unwrapRef(renderContext[key])
return unref(renderContext[key])
} else if (type.props != null) {
// only cache other properties when instance has declared (this stable)
// props

View File

@@ -3,6 +3,8 @@
export const version = __VERSION__
export {
ref,
unref,
shallowRef,
isRef,
toRefs,
reactive,