• 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
  • 相关阅读:
    分布式ID生成算法——雪花算法
    学习c#桌面应用编程 --- 我的第一个游戏
    DVWA-弱会话IDS
    【全志T113-S3_100ask】11-2编写驱动采集dht11数据(cdev、中断、锁)
    将Helm编写的Chart推送到Harbor仓库
    计算机网络第八章知识点回顾(自顶向下)
    中职 网络搭建与应用 DCN无线常用配置
    Java入门第三季
    浅谈泰山众筹合约系统开发逻辑技术方案详解(原理解析)
    Python学习笔记五之错误与异常处理、面向对象实例
  • 原文地址:https://blog.csdn.net/weixin_43932245/article/details/133906772