以下是一个通用的弹框组件,使用了Vue.js:

<template>
  <div v-if="show" class="modal">
    <div class="modal-content">
      <div class="modal-header">
        <h3>{{title}}</h3>
        <button @click="closeModal">X</button>
      </div>
      <div class="modal-body">
        <slot></slot>
      </div>
      <div class="modal-footer">
        <button @click="confirm">确定</button>
        <button @click="cancel">取消</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    show: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      default: '提示'
    }
  },
  methods: {
    closeModal() {
      this.$emit('close')
    },
    confirm() {
      this.$emit('confirm')
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>

<style>
.modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.5);
}

.modal-content {
  width: 300px;
  background-color: white;
  border-radius: 5px;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #ccc;
}

.modal-body {
  padding: 10px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 10px;
  background-color: #f5f5f5;
  border-top: 1px solid #ccc;
}

button {
  border: none;
  background-color: #007bff;
  color: white;
  padding: 5px 10px;
  border-radius: 5px;
  margin-left: 10px;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}
</style>

使用方法:

<template>
  <div>
    <button @click="showModal">打开弹框</button>
    <modal :show="show" @close="closeModal" @confirm="confirm" @cancel="cancel">
      <p>这是弹框内容</p>
    </modal>
  </div>
</template>

<script>
import Modal from './Modal.vue'

export default {
  components: {
    Modal
  },
  data() {
    return {
      show: false
    }
  },
  methods: {
    showModal() {
      this.show = true
    },
    closeModal() {
      this.show = false
    },
    confirm() {
      console.log('点击了确定按钮')
      this.show = false
    },
    cancel() {
      console.log('点击了取消按钮')
      this.show = false
    }
  }
}
</script>
``
使用vuejs编写一个通用的弹框组件

原文地址: https://www.cveoy.top/t/topic/dUqR 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录