vue3【组件封装】确认对话框 Modal

src/components/Modal.vue

html 复制代码
<template>
  <transition name="fade">
    <div
      class="fixed w-full h-full z-100 top-0 left-0 bg-black bg-opacity-30 flex transition-all"
      v-if="show"
    >
      <div class="w-80 m-auto bg-white border shadow-lg rounded-2 px-4 py-6">
        <slot></slot>
        <div class="flex justify-end pt-4">
          <div class="btn-plain px-6 mr-4" @click="show = false">取消</div>
          <div
            class="btn px-6"
            @click="
              () => {
                show = false
                $emit('confirm')
              }
            "
          >
            确定
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
defineEmits(['confirm'])
const show = defineModel({ default: false })
</script>

使用

html 复制代码
  <button @click="show_confirmWin = true">删除</button>

  <teleport to="body">
    <Modal v-model="show_confirmWin" @confirm="del"> 确定删除吗? </Modal>
  </teleport>
ts 复制代码
const show_confirmWin = ref(false)
const del = () => {
  alert('删除成功!')
}
相关推荐
Irene199120 小时前
通用消息组件 bug 修复及更好的实现是使用函数调用组件
vue3·函数调用·通用消息组件
Irene19912 天前
Vuex4:专为 Vue 3 设计,提供完整 TypeScript 支持
vue3·vuex4
无法长大2 天前
如何判断项目需不需要用、能不能用Tailwind CSS
前端·css·vue.js·elementui·vue3·tailwind css
cui_win3 天前
企业级中后台开源解决方案汇总
开源·vue3·ts
Sapphire~4 天前
Vue3-19 hooks 前端数据和方法的封装
前端·vue3
記億揺晃着的那天4 天前
Vue3 动态路由在生产环境才出现白屏的排查与解决(keep-alive 踩坑实录)
vue3·vue router·动态路由·生产环境报错
kong79069288 天前
Vue3快速入门
前端·vue3
无法长大9 天前
Mac M1 环境下使用 Rust Tauri 将 Vue3 项目打包成 APK 完整指南
android·前端·macos·rust·vue3·tauri·打包apk
淡笑沐白10 天前
Vue3使用ElementPlus实现菜单的无限递归
javascript·vue3·elementplus
Sapphire~10 天前
Vue3-18 生命周期(vue2+vue3)
vue3