vue2 以及 vue3 自定义组件使用 v-model使用默认值以及自定义事件

vue2 以及 vue3 自定义组件使用 v-model使用默认值以及自定义事件

1. vue2 自定义组件的 v-model

  1. vue2官网,自定义组件
  2. 官方解释:一个组件上的 v-model 默认会利用名为 value 的 prop 和名为 input 的事件
  3. 上代码
  4. 代码中使用了 element-ui

子组件 使用默认 value 和input 事件


// dialog.vue

<template>
  <el-dialog
    title="提示"
    :visible.sync="value"
    width="30%"
    :before-close="handleClose"
  >
    <span>2222222</span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
   
  props: {
   
    value: {
   
      type: Boolean,
      default: () => false,
    },
  },
  methods: {
   
    // 取消
    handleClose() {
   
      // 弹窗关闭 默认的event事件为 input
      this.$emit("input", false);
    },
    // 提交
    submit() {
   },
  },
};
</script>

父组件使用

<template>
  <div>
    <el-button type="primary" @click="visible = true">打开弹窗</el-button> 
    <Dialog v-model="visible"></Dialog>
  </div>
</template>
<script>
import Dialog from "@/components/dialog.vue";
export default {
   
  components: {
   
    Dialog,
  },
  data() {
   
    return {
   
      visible: false,
    };
  },
  methods:{
   
    changeDialog(data){
   
       console.log(data)
       this.text = data
    },
  }
};
</script>
  1. 官方解释后半段:自定义 prop 和 事件名 像单选框、复选框等类型的输入控件可能会将 value attribute 用于不同的目的。model 选项可以用来避免

子组件

<template>
  <el-dialog
    title="提示"
    :visible.sync="visible"
    width="30%"
    :before-close="handleClose"
  >
    <span>2222222</span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
   
  model: {
   
    prop: "visible",
    event: "update", // 为了避免事件名称冲突 此处可以自定义方法名
  },
  props: {
   
    visible: {
   
      type: Boolean,
      default: () => false,
    },
  },
  data() {
   
    return {
   };
  },
  methods: {
   
    // 取消
    handleClose() {
   
      //   使用自定义方法名 和上面这行代码效果一致
      this.$emit("update", false);
    },
    // 提交
    submit() {
   },
  },
};
</script>

父组件

<template>
  <div>
    <el-button type="primary" @click="visible = true">打开弹窗</el-button> 
    <Dialog v-model="visible"></Dialog>
  </div>
</template>
<script>
import Dialog from "@/components/dialog.vue";
export default {
   
  components: {
   
    Dialog,
  },
  data() {
   
    return {
   
      visible: false,
    };
  },
  methods:{
   
    changeDialog(data){
   
       console.log(data)
       this.text = data
    },
  }
};
</script>

2. vue3 自定义组件的 v-model

  1. vue3官网,自定义组件
  2. 官方解释:默认情况下,v-model 在组件上都是使用 modelValue 作为 prop,并以 update:modelValue 作为对应的事件
  3. 上代码
  4. 代码中使用了 element-plus

子组件 使用默认 modelValue 和 update:modelValue事件

// 子组件   dialog.vue
<template>
  <el-dialog
    v-model="props.modelValue"
    title="Tips"
    width="30%"
    :before-close="handleClose"
  >
    <span>This is a message</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
      
<script setup lang="ts">
// 接受数据
const props = defineProps({
   
  modelValue: {
   
    type: Boolean,
    default: () => false,
  },
});

// 注册事件
const emit = defineEmits(["update:modelValue"]);

// 关闭
const handleClose = () => {
   
  emit("update:modelValue", false);
};
// 提交
const submit = () => {
   };
</script>

父组件

<template>
  <div>
    <el-button text @click="visible = true"> 打开弹窗 </el-button>
    <!-- 第一种方式 使用默认方式 -->
    <Dialog v-model="visible"></Dialog>
  </div>
</template>
    
<script setup lang="ts">
import Dialog from "@/components/dialog.vue";
import {
    ref } from "vue";

const visible = ref(false);
</script>
  1. 官方解释后半段:自定义 prop 和 事件名 我们可以通过给 v-model 指定一个参数来更改这些名字

子组件

<template>
  <el-dialog
    v-model="props.visible"
    title="Tips"
    width="30%"
    :before-close="handleClose"
  >
    <span>This is a message</span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="submit"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
      
<script setup lang="ts">
// 接受数据
const props = defineProps({
   
    visible: {
   
    type: Boolean,
    default: () => false,
  },
});

// 注册事件
const emit = defineEmits(["update:visible"]);

// 关闭
const handleClose = () => {
   
  emit("update:visible", false);
};
// 提交
const submit = () => {
   };
</script>

父组件

<template>
  <div>
    <el-button text @click="visible = true"> 打开弹窗 </el-button>
    <!-- 第二种方式 自定义props 名称为 visible -->
    <Dialog v-model:visible="visible"></Dialog>
  </div>
</template>
    
<script setup lang="ts" name="debounceDirect">
import Dialog from "@/components/dialog.vue";
import {
    ref } from "vue";

const visible = ref(false);
</script>

以上就是vue2或者vue3 自定义组件 v-model 的使用!

相关推荐

  1. vue3定义组件使用v-model

    2023-12-15 09:00:02       13 阅读
  2. vue3定义多个v-model以及定义修饰符

    2023-12-15 09:00:02       13 阅读
  3. vue3利用定义事件v-model实现父子传参

    2023-12-15 09:00:02       37 阅读
  4. vue使用定义组件规则

    2023-12-15 09:00:02       14 阅读
  5. vue3使用定义组件内方法

    2023-12-15 09:00:02       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 09:00:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 09:00:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 09:00:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 09:00:02       20 阅读

热门阅读

  1. 【数据库设计和SQL基础语法】--查询数据--排序

    2023-12-15 09:00:02       34 阅读
  2. Web3.0和WebAssembly

    2023-12-15 09:00:02       30 阅读
  3. 【影像组学入门百问】#22—#24

    2023-12-15 09:00:02       33 阅读
  4. Git 的基本概念和使用方式。

    2023-12-15 09:00:02       35 阅读
  5. STM32系统滴答定时器SysTick实现精确ms和us延时

    2023-12-15 09:00:02       37 阅读
  6. 路由和网络周期

    2023-12-15 09:00:02       35 阅读
  7. android 13.0 Launcher3禁止拖拽app图标到第一屏

    2023-12-15 09:00:02       39 阅读
  8. 苏银消金大手笔增资,江苏银行持股比例上升

    2023-12-15 09:00:02       43 阅读
  9. 【bash】Bash脚本基础语法学习

    2023-12-15 09:00:02       34 阅读
  10. Bash script进阶笔记

    2023-12-15 09:00:02       34 阅读
  11. Python多线程编程:竞争问题的解析与应对策略

    2023-12-15 09:00:02       37 阅读