componentProxy.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ComponentInstance } from './component'
  2. export const RenderProxyHandlers = {
  3. get(target: ComponentInstance, key: string) {
  4. const { state, props } = target
  5. if (state.hasOwnProperty(key)) {
  6. return state[key]
  7. } else if (props.hasOwnProperty(key)) {
  8. return props[key]
  9. } else {
  10. switch (key) {
  11. case '$state':
  12. return target.state
  13. case '$props':
  14. return target.props
  15. case '$attrs':
  16. return target.attrs
  17. case '$slots':
  18. return target.slots
  19. case '$refs':
  20. return target.refs
  21. default:
  22. break
  23. }
  24. }
  25. },
  26. set(target: ComponentInstance, key: string, value: any): boolean {
  27. const { state } = target
  28. if (state.hasOwnProperty(key)) {
  29. state[key] = value
  30. return true
  31. } else {
  32. if (__DEV__) {
  33. if (key[0] === '$') {
  34. // TODO warn attempt of mutating public property
  35. } else if (target.props.hasOwnProperty(key)) {
  36. // TODO warn attempt of mutating prop
  37. }
  38. }
  39. }
  40. return false
  41. }
  42. }