props.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { isArray, Data } from '@vue/shared'
  2. import { inject } from '../apiInject'
  3. import { ComponentInternalInstance } from '../component'
  4. import { ComponentOptions, resolveMergedOptions } from '../componentOptions'
  5. import { DeprecationTypes, warnDeprecation } from './compatConfig'
  6. export function createPropsDefaultThis(
  7. instance: ComponentInternalInstance,
  8. rawProps: Data,
  9. propKey: string
  10. ) {
  11. return new Proxy(
  12. {},
  13. {
  14. get(_, key: string) {
  15. __DEV__ &&
  16. warnDeprecation(DeprecationTypes.PROPS_DEFAULT_THIS, null, propKey)
  17. // $options
  18. if (key === '$options') {
  19. return resolveMergedOptions(instance)
  20. }
  21. // props
  22. if (key in rawProps) {
  23. return rawProps[key]
  24. }
  25. // injections
  26. const injections = (instance.type as ComponentOptions).inject
  27. if (injections) {
  28. if (isArray(injections)) {
  29. if (injections.includes(key)) {
  30. return inject(key)
  31. }
  32. } else if (key in injections) {
  33. return inject(key)
  34. }
  35. }
  36. }
  37. }
  38. )
  39. }