美文网首页
hoos封装:computed拦截v-model

hoos封装:computed拦截v-model

作者: Ving | 来源:发表于2023-10-18 14:20 被阅读0次

    useVModel.js

    import { computed } from "vue";
    
    export default function useVModle(props, propName, emit) {
        return computed({
            get() {
                return new Proxy(props[propName], {
                    get(target, key) {
                        return Reflect.get(target, key)
                    },
                    set(target, key, newValue) {
                        emit('update:' + propName, {
                            ...target,
                            [key]: newValue
                        })
                        return true
                    }
                })
            },
            set(value) {
                emit('update:' + propName, value)
            }
        })
    }
    

    组件使用

    <!-- 父组件 -->
    <template>
      <div>
        <my-component v-model="form"></my-component>
      </div>
    </template>
    <script setup>
    import myComponent from "./components/MyComponent.vue";
    
    const form = ref({
      name: "张三",
      age: 18,
      sex: "man",
    });
    
    watch(form, (newValue) => {
      console.log(newValue);
    });
    </script>
    
    
    <!-- 子组件 -->
    <template>
      <div>
        <el-input v-model="form.name"></el-input>
        <el-input v-model="form.age"></el-input>
        <el-input v-model="form.sex"></el-input>
      </div>
    </template>
    <script setup>
    import useVModel from "../hooks/useVModel";
    
    const props = defineProps({
      modelValue: {
        type: Object,
        default: () => {},
      },
    });
    
    const emit = defineEmits(["update:modelValue"]);
    
    const form = useVModel(props, "modelValue", emit);
    
    </script>
    

    参考原文:https://juejin.cn/post/7277089907974422588

    相关文章

      网友评论

          本文标题:hoos封装:computed拦截v-model

          本文链接:https://www.haomeiwen.com/subject/swkqidtx.html