vue3-yuanma/packages/runtime-core/src/componentProxy.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-05-29 13:43:46 +08:00
import { ComponentInstance } from './component'
export const RenderProxyHandlers = {
get(target: ComponentInstance, key: string) {
2019-06-19 17:08:42 +08:00
const { data, props } = target
if (data.hasOwnProperty(key)) {
return data[key]
2019-05-29 13:43:46 +08:00
} else if (props.hasOwnProperty(key)) {
return props[key]
} else {
switch (key) {
2019-06-19 17:08:42 +08:00
case '$data':
return data
2019-05-29 13:43:46 +08:00
case '$props':
2019-06-19 17:08:42 +08:00
return props
2019-05-29 13:43:46 +08:00
case '$attrs':
return target.attrs
case '$slots':
return target.slots
case '$refs':
return target.refs
2019-06-03 13:44:45 +08:00
case '$parent':
return target.parent
case '$root':
return target.root
2019-06-19 16:43:34 +08:00
case '$emit':
return target.emit
2019-05-29 13:43:46 +08:00
default:
2019-08-21 21:50:20 +08:00
return target.user[key]
2019-05-29 13:43:46 +08:00
}
}
},
set(target: ComponentInstance, key: string, value: any): boolean {
2019-06-19 17:08:42 +08:00
const { data } = target
if (data.hasOwnProperty(key)) {
data[key] = value
2019-05-29 13:43:46 +08:00
return true
2019-08-21 21:50:20 +08:00
} else if (key[0] === '$' && key.slice(1) in target) {
// TODO warn attempt of mutating public property
return false
} else if (key in target.props) {
// TODO warn attempt of mutating prop
return false
2019-05-29 13:43:46 +08:00
} else {
2019-08-21 21:50:20 +08:00
target.user[key] = value
return true
2019-05-29 13:43:46 +08:00
}
}
}