vue2和vue3中的v-model

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>

相关推荐

  1. vue2vue3v-model

    2024-03-30 08:32:05       45 阅读
  2. Vue2 Vue3 v-model 区别#记录

    2024-03-30 08:32:05       38 阅读
  3. Vue技巧】Vue2Vue3组件上使用v-model实现原理

    2024-03-30 08:32:05       58 阅读
  4. Vue2v-model

    2024-03-30 08:32:05       43 阅读
  5. 详解vue3组件 v-model

    2024-03-30 08:32:05       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-30 08:32:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-30 08:32:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-30 08:32:05       82 阅读
  4. Python语言-面向对象

    2024-03-30 08:32:05       91 阅读

热门阅读

  1. RTSP服务端的响应格式

    2024-03-30 08:32:05       38 阅读
  2. 各类Springboot管理系统源码获取

    2024-03-30 08:32:05       32 阅读
  3. 5.98 BCC工具之biotop.py解读

    2024-03-30 08:32:05       33 阅读
  4. 【使用 PyQt6-第02章】 创建信号、槽和事件

    2024-03-30 08:32:05       30 阅读
  5. 5.97 BCC工具之biolatency.py解读

    2024-03-30 08:32:05       37 阅读
  6. Mac更换JDK版本

    2024-03-30 08:32:05       36 阅读
  7. 每天一个数据分析题(二百四十一)

    2024-03-30 08:32:05       47 阅读
  8. Python爬虫之数据的存储

    2024-03-30 08:32:05       34 阅读
  9. C++ 让类只在堆或栈上分配

    2024-03-30 08:32:05       45 阅读
  10. 搭建vite+vue3项目时遇到的问题

    2024-03-30 08:32:05       44 阅读