Ben Lin
2024-05-29 c89adfd5c5df23b6b102766092867adc2330ebb7
1
2
3
4
5
6
7
8
9
10
11
12
13
//useVmodel2.js
import { toRef, watch, getCurrentInstance } from 'vue';
export function useVmodel(props, key = 'modelValue', emit) {
  const vm = getCurrentInstance();
  const _emit = emit || vm?.emit;
  const event = `update:${key}`;
  const proxy = toRef(props, key); //ref(props[key]);
  watch(
    () => proxy.value,
    (v) => _emit(event, v),
  );
  return proxy;
}