1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import { computed } from "vue"; import type { WritableComputedRef } from "vue";
export function useVModel<P extends object, K extends string & keyof P>( props: P, propName: K, emit: (event: `update:${K}`, ...args: any[]) => void ) { return computed({ get() { if ( Object.prototype.toString.call(props[propName]) === "[object Object]" ) { return new Proxy(props[propName] as object, { set(obj, name, val) { emit(`update:${propName}`, Object.assign(obj, { [name]: val })); return true; }, }); } return props[propName]; }, set(val) { emit(`update:${propName}`, val); }, }); }
export function useVModels<P extends object, Name extends string>( props: P, emit?: (name: Name, ...args: any[]) => void ) { const ret = {} as { [K in keyof P]: WritableComputedRef<object | P[K]> }; for (const key in props) ret[key] = useVModel(props, key, emit as any) return ret; }
|