vue2和vue3中的v-model

本文详细介绍了Vue中如何使用v-model在父组件与子组件间传递值并保持同步,包括默认的prop和event设置,以及Vue2.3之后的.sync修饰符和Vue3中弃用.sync的处理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

v-model常用在父组件给子组件传值,而子组件的值发生变化时,也想让父组件的这个值同步发生变化时。例如,要控制el-dialog的visible,在父组件调用这个弹窗时,把控制显藏的变量传到子组件,在子组件里要关闭弹窗时是需要emit到父组件把变量置为false的,这个时候就可以用到v-model代替一般的传值,当然用其他的传值方式也可以。

  • vue2的v-model默认给子组件传的值是value,事件是input。代码示例

父组件

<template>
    <div>
        值是{{count}}

        <!-- <SonPage v-model:value="count" @input= val => count = val></SonPage> -->
        <!-- 上面的写法等同于下面 -->

        <SonPage v-model="count"></SonPage>
    </div>
</template>

<script>
import SonPage from './SonPage.vue';

export default{
    name: 'fatherPage',

    components: { SonPage },

    data() {
        return {
            count: 1
        }
    },
}

</script>

子组件

<template>
    <div>
        {{number}}----number

        <button @click="submit()">点击</button>
    </div>
</template>

<script>
export default {
    name: 'sonPage',
    props: ['value'],
    data() {
        const {value} = this.$props;
        return {
            number: value
        }
    },

    methods: {
        submit() {
            this.number = this.number+1;
            this.$emit('input', this.number)
        }
    }
}
</script>
  • v-model传递给子组件的prop默认名称为value,事件默认为input,如果想要修改这两个默认值,可以使用model来重新指定,如上面的例子中,指定v-model传递给子组件的名称为info,事件为change(随便写)
export default {
    name: 'sonPage',

    model: {
        prop: 'info',
        event: 'change'
    },

    props: ['info'],
    data() {
        const {info} = this.$props;
        return {
            number: info
        }
    },

    methods: {
        submit() {
            this.number = this.number+1;
            this.$emit('change', this.number)
        }
    }
}
  • .sync (vue2.3之后的版本)

v-model 只能针对一个变量进行数据绑定,而 .sync 修饰符可以实现多个参数的数据双向绑定。
而且使用.sync也可以简化写法

<template>
    <div>
        值是{{count}}

        <!-- <SonPage :info="count" @update:info = val => count = val></SonPage>  -->
        <!-- 上面等价于下面 -->
        <SonPage :info.sync="count"></SonPage>
    </div>
</template>

<script>
import SonPage from './SonPage.vue';

export default{
    name: 'fatherPage',

    components: { SonPage },

    data() {
        return {
            count: 1
        }
    },
}

</script>
<template>
    <div>
        {{number}}----number

        <button @click="submit()">点击</button>
    </div>
</template>

<script>
export default {
    name: 'sonPage',

    props: ['info'],
    data() {
        const {info} = this.$props;
        return {
            number: info
        }
    },

    methods: {
        submit() {
            // this.number = this.number+1;
            this.$emit('update:info', this.number+1)
        }
    }
}
</script>
  • vue3版本,摒弃了.sync的写法
<template>
    <div>   
        {{count}}

        {{ name }}

        <son v-model:info="count" v-model:name="name"></son>
    </div>
</template>

<script setup>
import { ref } from 'vue';
import son from './son.vue';
  const count = ref(1);
  const name = ref('小明');


</script>
<template>
    <div>
        {{ val }}--val

        {{ name }}--name
        <button @click="submit">点击</button>
    </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';

const props = defineProps({
    info: {
        type: Number,
        require: true
    },
    name: {
        type: String,
        require: true
    }
});

const val = ref(props.info)
const name = ref(props.name)

const emit = defineEmits(['update:info', 'update:name']);


const submit = () => {
    emit('update:info', 2);
    emit('update:name', '大名');

}
</script>
### Vue2Vue3 中 v-model 的实现方式与区别 在 Vue 框架中,`v-model` 是实现双向绑定的核心功能。以下是 Vue2 Vue3 中 `v-model` 的实现方式及其差异的详细分析。 #### 1. Vue2 中 `v-model` 的实现 在 Vue2 中,`v-model` 主要通过 `props` 自定义事件 `input` 实现双向绑定。当父组件使用 `v-model` 时,会将数据传递给子组件的 `props.value` 属性,并监听子组件触发的 `input` 事件来更新父组件的数据[^2]。例如: ```html <!-- 父组件 --> <child-component v-model="parentValue"></child-component> <!-- 子组件 --> <script> export default { props: [&#39;value&#39;], // 接收父组件传递的值 methods: { updateValue(newVal) { this.$emit(&#39;input&#39;, newVal); // 触发 input 事件以更新父组件数据 } } } </script> ``` 这种实现方式简单直接,但存在一定的局限性,特别是在需要支持多个 `v-model` 绑定时,Vue2 的设计显得不够灵活。 #### 2. Vue3 中 `v-model` 的实现 Vue3 对 `v-model` 进行了重构,增强了其灵活性可扩展性。在 Vue3 中,`v-model` 默认绑定到 `modelValue` 属性,并通过 `update:modelValue` 事件更新父组件的数据[^1]。例如: ```html <!-- 父组件 --> <child-component v-model="parentValue"></child-component> <!-- 子组件 --> <script> export default { props: [&#39;modelValue&#39;], // 接收父组件传递的值 emits: [&#39;update:modelValue&#39;], // 声明触发的事件 methods: { updateValue(newVal) { this.$emit(&#39;update:modelValue&#39;, newVal); // 触发 update:modelValue 事件 } } } </script> ``` 此外,Vue3 支持多 `v-model` 绑定,允许子组件同时绑定多个属性并触发对应的更新事件。例如,以下是一个多 `v-model` 的示例[^3]: ```html <!-- 父组件 --> <user-name v-model:firstName="firstName" v-model:lastName="lastName"></user-name> <!-- 子组件 --> <script setup> defineProps({ firstName: String, lastName: String }); defineEmits([&#39;update:firstName&#39;, &#39;update:lastName&#39;]); </script> <template> <input type="text" :value="firstName" @input="$emit(&#39;update:firstName&#39;, $event.target.value)" /> <input type="text" :value="lastName" @input="$emit(&#39;update:lastName&#39;, $event.target.value)" /> </template> ``` #### 3. Vue2Vue3 的主要差异 - **默认属性名称**:在 Vue2 中,默认绑定到 `props.value`,而在 Vue3 中,默认绑定到 `props.modelValue`[^2]。 - **事件名称**:在 Vue2 中,触发的事件为 `input`,而在 Vue3 中,触发的事件为 `update:modelValue`。 - **多 `v-model` 支持**:Vue3 提供了对多 `v-model` 的原生支持,而 Vue2 需要通过自定义事件实现类似功能[^3]。 - **性能优化**:Vue3 对事件系统进行了优化,减少了不必要的开销,提升了性能。 #### 4. 总结 Vue3 在 `v-model` 的设计上更加灵活现代化,不仅保留了 Vue2 的核心理念,还通过引入 `modelValue` `update:modelValue` 提升了代码的可读性扩展性。此外,多 `v-model` 的支持使得复杂场景下的双向绑定变得更加直观高效。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值