toReactive.js 825 B

1234567891011121314151617181920212223242526272829303132333435
  1. import { isRef, reactive } from 'vue';
  2. /**
  3. * Converts ref to reactive.
  4. *
  5. * @see https://vueuse.org/toReactive
  6. * @param objectRef A ref of object
  7. */
  8. export function toReactive(objectRef) {
  9. if (!isRef(objectRef)) return reactive(objectRef);
  10. const proxy = new Proxy({}, {
  11. get(_, p, receiver) {
  12. return Reflect.get(objectRef.value, p, receiver);
  13. },
  14. set(_, p, value) {
  15. objectRef.value[p] = value;
  16. return true;
  17. },
  18. deleteProperty(_, p) {
  19. return Reflect.deleteProperty(objectRef.value, p);
  20. },
  21. has(_, p) {
  22. return Reflect.has(objectRef.value, p);
  23. },
  24. ownKeys() {
  25. return Object.keys(objectRef.value);
  26. },
  27. getOwnPropertyDescriptor() {
  28. return {
  29. enumerable: true,
  30. configurable: true
  31. };
  32. }
  33. });
  34. return reactive(proxy);
  35. }