vue封装弹框组件

效果图:
在这里插入图片描述

/* eslint-disable no-irregular-whitespace */
<template>
  <div class="paymentdetail_compontents" v-if="isShowDialogs">
    <div class="paymentdetail_div_box">
      <div class="paymentdetail_div1">
        <span class="paymentdetail_div1_warn">提示</span>

        <div class="paymentdetail_person_box">{{ Message }}</div>
      </div>

      <div class="paymentdetail_div2">
        <span class="paymentdetail_div2_span" @click="isShowRefruct">取消</span>

        <span class="paymentdetail_div2_span1" @click="isShowAgree">确定</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "alertbox",
  //使用props接收从父组件中接收的值
  props: ["isshowAlert", "Message"],
  data() {
    return {
      isShowDialogs: true
    };
  },
  methods: {
    isShowRefruct() {
      this.isShowDialogs = false;
      this.$emit("replaceChecked", "replace");
    },
    isShowAgree() {
      this.isShowDialogs = false;
      this.$emit("sureChecked", this.Message);
    }
  },
  //watch中监听弹框是否出现
  watch: {
    isshowAlert() {
      this.isShowDialogs = this.isshowAlert;
    }
  }
};
</script>

<style lang="scss" scoped>
.paymentdetail_compontents {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 33333;
  background-color: rgba(0, 0, 0, 0.6);
  .paymentdetail_div_box {
    width: 300px;
    background-color: white;
    border-radius: 5px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .paymentdetail_div1 {
      height: 90px;
      text-align: center;
      margin-top: 20px;
      .paymentdetail_person_box {
        height: 50px;
        margin-top: 20px;
        color: #685c5c;
      }
      .paymentdetail_div1_warn {
        color: #000;
        font-family: bolder;
      }
    }
    .paymentdetail_div2 {
      border-top: 1px solid #eee;
      /* display: flex; */
      span {
        /* flex: 1; */
        height: 39px;
        line-height: 39px;
        color: #5077aa;
        text-align: center;
        width: 50%;
      }
      .paymentdetail_div2_span {
        border-right: 1px solid #eee;
        color: #685c5c;
        float: left;
        box-sizing: border-box;
      }
      .paymentdetail_div2_span1 {
        float: right;
      }
    }
  }
}
</style>

在父组件中引入

import alertbox from "@/components/alertbox";
components: { alertbox }
<alertbox
      :isshowAlert="showAlert"
      :Message="alertMessage"
      @replaceChecked="replace_checked"
      @sureChecked="sure_checked"
    ></alertbox>
data() {
    return {
      showAlert: "",
      alertMessage: "弹框"
    };
  },
methods: {
    replace_checked() {
      console.log("你点击了取消按钮");
    },

    sure_checked() {
      console.log("你点击了确定按钮");
    }
  },
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,供参考: ```vue <template> <div> <button @click="showDialog">打开弹框</button> <Dialog v-model="dialogVisible"> <h2 slot="title">标题</h2> <div slot="content"> <Table :columns="columns" :data="tableData"></Table> </div> </Dialog> </div> </template> <script> import Dialog from './Dialog.vue' import Table from './Table.vue' export default { components: { Dialog, Table }, data() { return { dialogVisible: false, tableData: [ { name: '张三', age: 20, job: 'Web 开发工程师' }, { name: '李四', age: 25, job: '移动端开发工程师' }, { name: '王五', age: 30, job: '前端架构师' }, ], columns: [ { title: '姓名', key: 'name' }, { title: '年龄', key: 'age' }, { title: '职业', key: 'job' }, ], } }, methods: { showDialog() { this.dialogVisible = true }, }, } </script> ``` 其中,Dialog 和 Table 是两个子组件,代码如下: ```vue <!-- Dialog.vue --> <template> <div class="dialog-mask" v-show="value"> <div class="dialog"> <div class="dialog-header"> <slot name="title">提示</slot> <button class="dialog-close" @click="close"> <i class="el-icon-close"></i> </button> </div> <div class="dialog-body"> <slot name="content"></slot> </div> </div> </div> </template> <script> export default { props: { value: { type: Boolean, default: false, }, }, methods: { close() { this.$emit('input', false) }, }, } </script> <style> .dialog-mask { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 999; } .dialog { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 500px; height: 300px; background-color: #fff; border-radius: 4px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } .dialog-header { display: flex; align-items: center; justify-content: space-between; padding: 10px; font-size: 20px; font-weight: bold; border-bottom: 1px solid #eee; } .dialog-close { border: none; background-color: transparent; font-size: 16px; color: #999; cursor: pointer; outline: none; } .dialog-close:hover { color: #333; } .dialog-body { padding: 10px; overflow: auto; } </style> ``` ```vue <!-- Table.vue --> <template> <table> <thead> <tr> <th v-for="column in columns" :key="column.key">{{ column.title }}</th> </tr> </thead> <tbody> <tr v-for="(item, index) in data" :key="index"> <td v-for="column in columns" :key="column.key">{{ item[column.key] }}</td> </tr> </tbody> </table> </template> <script> export default { props: { columns: { type: Array, default: () => [], }, data: { type: Array, default: () => [], }, }, } </script> <style> table { width: 100%; border-collapse: collapse; } th, td { padding: 10px; border: 1px solid #eee; } </style> ``` 以上代码仅供参考,具体实现还需要根据实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值