vue2实现自定义element对话框

 子组件中components/Dialogs.vue中

<template>

  <div class="commydialog" v-if="visable">

    <div class="mask"></div>

    <div class="comdialog_box">

      <div class="comdialog_box_top">

        <slot name="headsolt"></slot>

        <span @click="$emit('comcancel')" class="cuo">×</span>

      </div>

      <div class="comdialog_box_center">

        {{ content }}

      </div>

      <div class="comdialog_box_bottom">

        <button @click="$emit('suredialog')" class="ok">确定</button>

        <button @click="$emit('cancel')" class="cancel">取消</button>

      </div>

    </div>

  </div>

</template>



<script>

export default {

  props: {

    visable: {

      default: true,

      type: Boolean,

    },

    content: {

      default: '提示',

      type: String,

    },

  },

  components: {},

  data() {

    return {}

  },

  created() {},

  mounted() {},

  methods: {},

}

</script>

<style scoped lang="scss">

.commydialog {

  width: 100%;

  height: 100vh;

  position: relative;

  .mask {

    width: 100%;

    height: 100%;

    background-color: rgba(0, 0, 0, 0.3);

    position: absolute;

    top: 0;

    left: 0;

  }

  .comdialog_box {

    width: 400px;

    height: 200px;

    background-color: #fff;

    position: absolute;

    left: 50%;

    top: 50%;

    transform: translate(-50%, -50%);

    padding: 10px;

    .comdialog_box_top {

      position: relative;

      .cuo {

        position: absolute;

        right: 20px;

        // top: 20px;

        font-size: 30px;

        color: #ccc;

      }

    }

    .comdialog_box_center {

      height: 100px;

      line-height: 100px;

      background-color: #ffffff;

      margin: 10px 0;

    }

    .comdialog_box_bottom {

      width: 100%;

      height: 70px;

      // padding-top: 12px;

      padding-left: 170px;

      box-sizing: border-box;

      button {

        width: 65px;

        height: 38px;

        font-size: 16px;

        margin-left: 20px;

        border-radius: 2px;

        border: none;

        outline: none;

        cursor: pointer;

        border-radius: 10px;

      }

      .ok {

        background-color: #409eff;

        color: #fff;

      }

    }

  }

}

</style>

在父组件中进行引用dialog.vue

<template>
  <div>
    <!-- 
    实现思路
    1.添加一个遮罩层,然后点击按钮显示我们的对话框
    2.在父组件中引入我们的子组件
    3.在子组件中设置一个插槽,然后在父组件中定义对话框的标题
    4.在子组件中使用props接收父组件传递过来的数据
    5.使用$emit给他设置一个点击事件然后 在父组件中进行接收
    -->
    <button @click="opendialog" v-if="!visable">点击显示对话框</button>
    <myDialog
      :visable="visable"
      :content="content"
      @comcancel="visable = false"
      @cancel="visable = !visable"
      @suredialog="visable = false"
    >
      <template slot="headsolt">
        <span>提示</span>
      </template>
    </myDialog>
  </div>
</template>

<script>
import myDialog from '@/components/Dialogs.vue'
export default {
  components: {
    myDialog,
  },
  data() {
    return {
      visable: false,
      content: '主体部分默认值',
    }
  },
  created() {},
  mounted() {},
  methods: {
    opendialog() {
      this.visable = true
      this.content = '这是一段信息'
    },
  },
}
</script>
<style scoped lang="scss"></style>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值