vue 默认setup方式的v-model实现同时多个绑定,update:modelValue默认方式和非默认方式

138 篇文章 1 订阅

1、新建一个conform.vue 其内容如下:

<template>
  <div class="xtx-confirm"  v-if="modelValue" :class="{fade}">
    <div class="wrapper" :class="{fade}">
      <div class="header">
        <h3>{{title}}</h3>
        <a @click="cancel" href="JavaScript:;" >x</a>
      </div>
      <div class="body">
        <i class="iconfont icon-warning"></i>
        <span>{{text}}</span>
        <input type="text" :value="text" @input="input_change">
      </div>
      <div class="footer">
        <span @click="cancel" class="cancel">取消</span>
        <span @click="submit" class="submit">确认</span>
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'

const props = defineProps({
  title: {
      type: String,
      default: '温馨提示'
    },
  text: {
    type: String,
    default: ''
  },
  modelValue: {
    type: Boolean,
    default: false
  },
});
// 可以看出来update:modelValue这个是默认的即对应父标签中的v-model= 而这个v-model:text为非默认的
const emits = defineEmits(['update:modelValue','update:text'])
const input_change =(e)=>{
  emits('update:text', e.target.value)
}

// 对话框默认隐藏
const fade = ref(false)
// 组件渲染完毕后
onMounted(() => {
  // 过渡效果需要在元素创建完毕后延时一会加上才会触发
  setTimeout(() => {
    fade.value = true
  }, 0)
})
// 取消
const cancel = () => {
  emits('update:modelValue', false)
}
// 确认
const submit = () => {
  emits('update:modelValue', false)
}

</script>
<style scoped lang="less">
.xtx-confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0,0,0,0);
  //background: rgba(0,0,0,.5);
  &.fade {
    transition: all 0.4s;
    background: rgba(0,0,0,.5);
  }
  .wrapper {
    width: 400px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-60%);
    opacity: 0;
    //opacity: 1;
    &.fade {
      transition: all 0.4s;
      transform: translate(-50%,-50%);
      opacity: 1;
    }
    .header,.footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
    }
    .body {
      padding: 20px 40px;
      font-size: 16px;
      .icon-warning {
        color: red;
        margin-right: 3px;
        font-size: 16px;
      }
    }
    .footer {
      text-align: right;
      cursor: pointer;
      .cancel{
        margin-right: 20px;
        cursor: pointer;
      }
      .submit{
        cursor: pointer;
      }
      //.xtx-button {
      //  margin-left: 20px;
      //}
    }
    .header {
      position: relative;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>

2、在app.vue中使用,其内容如下:

<template>
  <van-button type="primary" @click="openMask">主要按钮</van-button>
  <Conform v-model="show" v-model:text="father_text" ></Conform>

</template>

<script setup>
import Conform from './components/conform.vue'
import {ref, watch} from "vue";
const show = ref(false)
const father_text = ref('father_text')
const openMask = ()=>{
  show.value = true
}
watch(father_text, (newVal)=>{
  // 监听看是否父组件改变了
  console.log('father_text: ', newVal);
})

watch(show, (newVal)=>{
  // 监听看是否父组件改变了
  console.log('newVal: ', newVal);
})
</script>

<style scoped lang="less">
</style>

3、效果如下:

在这里插入图片描述

可以看出来update:modelValue这个是默认的即对应父标签中的v-model= 而这个v-model:text为非默认的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值