• vue3学习(十一)--- v-model


    v-model 其实是一个语法糖 通过propsemit组合而成的

    v-model的更新

    vue2和vue3中的v-model是有一些差别的:

    1. prop:value 变为 modelValue
    2. 事件:input 变为 update:modelValue
    3. v-bind 的 .sync 修饰符和组件的 model 选项已移除
    4. 新增 支持多个v-model
    5. 新增 支持自定义修饰符 Modifiers
    父组件
    <template>
      <button @click="show = !show">开关{{show}}</button>
      <Dialog v-model="show"></Dialog>
    </template>
     
    <script setup lang='ts'>
    import Dialog from "./components/Dialog/index.vue";
    import {ref} from 'vue'
    const show = ref(false)
    </script>
     
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    子组件
    <template>
         <div v-if='propData.modelValue ' class="dialog">
             <div class="dialog-header">
                 <div>标题</div><div @click="close">x</div>
             </div>
             <div class="dialog-content">
                内容
             </div>
             
         </div>
    </template>
     
    <script setup lang='ts'>
     
    type Props = {
    	默认就叫modelValue
       modelValue:boolean
    }
     
    const propData = defineProps<Props>()
    
    emit的默认事件名称写法
    const emit = defineEmits(['update:modelValue'])
     
    const close = () => {
         emit('update:modelValue',false)
    }
     
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    多个 v-model 绑定

    父组件
    <template>
      <button @click="show = !show">开关{{show}} ----- {{title}}</button>
      <Dialog v-model:title='title' v-model="show"></Dialog>
    </template>
     
    <script setup lang='ts'>
    import Dialog from "./components/Dialog/index.vue";
    import {ref} from 'vue'
    const show = ref(false)
    const title = ref('我是标题')
    </script>
     
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    子组件
    <template>
         <div v-if='modelValue ' class="dialog">
             <div class="dialog-header">
                 <div>标题---{{title}}</div><div @click="close">x</div>
             </div>
             <div class="dialog-content">
                内容
             </div>
             
         </div>
    </template>
     
    <script setup lang='ts'>
     
    type Props = {
       modelValue:boolean,
       title:string
    }
     
    const propData = defineProps<Props>()
     
    const emit = defineEmits(['update:modelValue','update:title'])
     
    const close = () => {
         emit('update:modelValue',false)
         emit('update:title','我要改变')
    }
     
    </script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    自定义修饰符

    添加到组件 v-model 的修饰符将通过 modelModifiers prop 提供给组件。在下面的示例中,我们创建了一个组件,其中包含默认为空对象的 modelModifiers prop

    父组件
    <template>
      <button @click="show = !show">开关{{show}} ----- {{title}}</button>
      这里添加修饰符
      <Dialog v-model:title.flag='title' v-model="show"></Dialog>
    </template>
     
    <script setup lang='ts'>
    import Dialog from "./components/Dialog/index.vue";
    import {ref} from 'vue'
    const show = ref(false)
    const title = ref('我是标题')
    </script>
     
    <style>
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    <script setup lang='ts'>
     
    type Props = {
        modelValue: boolean,
        title?: string,
        默认写法
        modelModifiers?: {
            default: () => {}
        }
        用的是修饰符前面的参数 + 'Modifiers'的固定写法
        titleModifiers?: {
            default: () => {}
        }
     
    }
     
    const propData = defineProps<Props>()
    
     console.log(propData.modelModifiers) ===> //{}
     console.log(propData.titleModifiers) ===> //flag:true
    
    const emit = defineEmits(['update:modelValue', 'update:title'])
     
    const close = () => {
        console.log(propData.modelModifiers);
     
        emit('update:modelValue', false)
        emit('update:title', '我要改变')
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    AI智能分析网关V4智慧工厂视频智能监管与风险预警平台建设方案
    运行软件mfc100u.dll缺失是怎么办?mfc100u.dll丢失解决方法分享
    SpringBoot_快速入门
    MyBatis逆向工程 —【解决生成多个实体——解决映射文件重复出现BaseResultMap】
    Linux——文件描述符(fd)与重定向、dup/dup2
    Java ArrayLIst与顺序表
    【第54篇】知识蒸馏:Distilling the Knowledge in a Neural Network
    Linux ARM平台开发系列讲解(CAN) 2.14.1 CAN基础协议分析
    后缀数组复习
    如何在Github精准地搜索项目
  • 原文地址:https://blog.csdn.net/weixin_43932245/article/details/133906772