【Vue】sync修饰符

一、介绍

作用:可以实现 子组件父组件数据双向绑定,简化代码

简单理解:子组件可以修改父组件传过来的props值

特点:prop属性名,可以自定义,非固定为value

但是.sync没有v-model方便,所以当属性名确定为value,即表单元素的时候,就使用v-model

**场景 ** 封装弹框类的基础组件, visible属性 true显示 false隐藏

本质 .sync修饰符 就是 :属性名@update:属性名 合写


二、语法

父组件

// .sync写法
<BaseDialog :visible.sync="isShow" />
--------------------------------------
// 完整写法
<BaseDialog 
  :visible="isShow" 
  @update:visible="isShow = $event" 
/>

子组件

props: {
  visible: Boolean
},

this.$emit('update:visible', false)

三、代码示例

App.vue

<template>
  <div class="app">
    <button @click="openDialog">退出按钮</button>
    <!-- isShow.sync  => :isShow="isShow" @update:isShow="isShow=$event" -->
    <BaseDialog :isShow.sync="isShow"></BaseDialog>
  </div>
</template>

<script>
import BaseDialog from './components/BaseDialog.vue'
export default {
  data() {
    return {
      isShow: false,
    }
  },
  methods: {
    openDialog() {
      this.isShow = true
      // console.log(document.querySelectorAll('.box')); 
    },
  },
  components: {
    BaseDialog,
  },
}
</script>

<style>
</style>

BaseDialog.vue

<template>
  <div class="base-dialog-wrap" v-show="isShow">
    <div class="base-dialog">
      <div class="title">
        <h3>温馨提示:</h3>
        <button class="close" @click="closeDialog">x</button>
      </div>
      <div class="content">
        <p>你确认要退出本系统么?</p>
      </div>
      <div class="footer">
        <button>确认</button>
        <button>取消</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    isShow: Boolean,
  },
  methods:{
    closeDialog(){
      this.$emit('update:isShow',false)
    }
  }
}
</script>

<style scoped>
.base-dialog-wrap {
  width: 300px;
  height: 200px;
  box-shadow: 2px 2px 2px 2px #ccc;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 0 10px;
}
.base-dialog .title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid #000;
}
.base-dialog .content {
  margin-top: 38px;
}
.base-dialog .title .close {
  width: 20px;
  height: 20px;
  cursor: pointer;
  line-height: 10px;
}
.footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 26px;
}
.footer button {
  width: 80px;
  height: 40px;
}
.footer button:nth-child(1) {
  margin-right: 10px;
  cursor: pointer;
}
</style>

相关推荐

  1. C#修饰符

    2024-06-08 03:04:05       45 阅读
  2. .sync修饰符

    2024-06-08 03:04:05       39 阅读
  3. Vue 修饰符有哪些

    2024-06-08 03:04:05       42 阅读
  4. iOS基础之修饰符

    2024-06-08 03:04:05       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 03:04:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 03:04:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 03:04:05       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 03:04:05       20 阅读

热门阅读

  1. springboot获取当前数据库连接

    2024-06-08 03:04:05       11 阅读
  2. Lambda表达式与函数式接口

    2024-06-08 03:04:05       9 阅读
  3. Nginx实现缓存

    2024-06-08 03:04:05       7 阅读
  4. c# 身份证信息验证

    2024-06-08 03:04:05       10 阅读
  5. oracle常用经典SQL查询

    2024-06-08 03:04:05       11 阅读
  6. 理论学习-自动控制

    2024-06-08 03:04:05       7 阅读
  7. Python笔记 - generator方法

    2024-06-08 03:04:05       12 阅读
  8. Elixir学习笔记——模块和函数

    2024-06-08 03:04:05       11 阅读
  9. Skins

    Skins

    2024-06-08 03:04:05      9 阅读
  10. DolphinScheduler调度系统

    2024-06-08 03:04:05       7 阅读
  11. 【高频】如何优化一个SQL语句

    2024-06-08 03:04:05       6 阅读