@slan-health/taro-vueuse
Version:
taro vue版本hooks
42 lines (41 loc) • 1.03 kB
JavaScript
import { getCurrentInstance, ref, watch, computed, unref } from "vue";
function useVModel(props, key, emit, options = {}) {
const {
passive = false,
eventName,
deep = false,
defaultValue
} = options;
const vm = getCurrentInstance();
const _emit = emit || (vm == null ? void 0 : vm.emit);
let event = eventName;
if (!key) {
key = "modelValue";
}
event = eventName || event || `update:${key.toString()}`;
const getValue = () => unref(props[key]) != null ? props[key] : defaultValue;
if (passive) {
const proxy = ref(getValue());
watch(() => props[key], (v) => proxy.value = v);
watch(proxy, (v) => {
if (v !== props[key] || deep)
_emit == null ? void 0 : _emit(event, v);
}, {
deep
});
return proxy;
} else {
return computed({
get() {
return getValue();
},
set(value) {
_emit == null ? void 0 : _emit(event, value);
}
});
}
}
export {
useVModel as default
};
//# sourceMappingURL=index.js.map