项目环境

@vue/cli 4.5.8

最终效果

需求分析

显示/隐藏

点击遮罩层能否关闭

宽度和zIndex自定义

标题栏 -显示标题和关闭按钮

主体

底部 -内置取消和确定功能

前置知识

teleport通过其to属性可以把实例插入到对应的body

实现过程

搭建大体的html模版

<template>
<teleport to="body">
<div class="modal">
<div class="modal-mask"></div>
<div class="modal-content">
<div class="modal-header">
<slot name="header"></slot>
</div>
<div class="modal-body">
<slot></slot>
</div>
<div class="modal-footer" v-if="!footerHide">
<slot name="footer">
<button class="modal-button">取消</button>
<button class="modal-button modal-button-primary">确定</button>
</slot>
</div>
<div class="modal-close"></div>
</div>
</div>
</teleport>
</template>
<script lang="ts">
import { defineComponent } from "vue"; export default defineComponent({
name: "Modal",
props: {},
emits: [],
setup() {
return { };
},
});
</script>
<style lang="scss">
</style>

添加props和mehtods和css

<template>
<teleport to="body">
<div class="modal" v-show="modelValue" :style="{ zIndex: zIndex }">
<div class="modal-mask" @click="maskClose"></div>
<div class="modal-content" :style="{ width: width }">
<div class="modal-header">
<slot name="header">{{ title }}</slot>
</div>
<div class="modal-body">
<slot></slot>
</div>
<div class="modal-footer" v-if="!footerHide">
<slot name="footer">
<button class="modal-button" @click="closeModal('cancel')">取消</button>
<button class="modal-button modal-button-primary" @click="sure">确定</button>
</slot>
</div>
<div class="modal-close" @click="closeModal('close')"></div>
</div>
</div>
</teleport>
</template>
<script lang="ts">
import { defineComponent } from "vue"; export default defineComponent({
name: "Modal",
props: {
modelValue: Boolean,
title: String,
footerHide: Boolean,
width: {
type: String,
default: "500px",
},
maskClosable: {
type: Boolean,
default: true,
},
zIndex: {
type: Number,
default: 1000,
},
},
emits: ["ok", "close", "update:modelValue"], // 方便TS推断
setup(props, { emit }) {
const closeModal = (type: string) => {
// 关闭Modal 并触发自定义事件‘close-有参数方便区分点击右上方的关闭按钮还是点击底部的取消’
};
const maskClose = () => {
// 通过点击mask层关闭Modal };
const sure = () => {
// 点击确定按钮关闭Modal并添加自定义事件‘ok’
};
return { closeModal, sure, maskClose };
},
});
</script>
<style lang="scss">
.modal {
position: fixed;
top: 0;
left: 0;
z-index: 1000;
&-mask {
width: 100vw;
height: 100vh;
background-color: rgba($color: #000000, $alpha: 0.4);
}
&-content {
width: 500px;
position: absolute;
top: 8vh;
left: 50%;
margin-left: -250px;
background-color: #fff;
border-radius: 8px;
z-index: 1;
font-size: 14px;
}
&-header {
padding: 12px 16px;
border-bottom: 1px solid #e4e7ed;
}
&-footer {
padding: 12px 16px;
border-top: 1px solid #e4e7ed;
text-align: right;
}
&-body {
padding: 16px;
}
&-close {
position: absolute;
top: 12px;
right: 12px;
width: 16px;
height: 16px;
cursor: pointer;
&::before,
&::after {
content: "";
display: block;
position: absolute;
left: 8px;
top: 0;
width: 1px;
height: 16px;
background-color: #999;
border-radius: 0.5px;
transform: rotate(-45deg);
z-index: -1;
}
&::before {
transform: rotate(45deg);
}
&:hover::before,
&:hover::after {
background-color: #444;
}
}
&-button {
line-height: 1em;
font-size: 14px;
padding: 8px 20px;
border: 1px solid #dcdfe6;
outline: none;
display: inline-block;
border-radius: 4px;
cursor: pointer;
background-color: #fff;
transition: 0.1s;
&:hover {
color: #409eff;
border-color: #c6e2ff;
background-color: #ecf5ff;
}
& + & {
margin-left: 10px;
} &-primary {
background-color: #2d8cf0;
border-color: #2d8cf0;
color: white;
&:hover {
background: #66b1ff;
border-color: #66b1ff;
color: #fff;
}
}
}
}
</style>

添加closeModal, sure, maskClose的具体实现--setup具体的实现

...
setup(props, { emit }) {
const closeModal = (type: string) => {
emit("update:modelValue", false);
emit("close", type);
};
const maskClose = () => {
if (props.maskClosable) closeModal("close");
};
const sure = () => {
emit("update:modelValue", false);
emit("ok");
};
return { closeModal, sure, maskClose };
},
...

完整的代码在github

vue3:modal组件开发的更多相关文章

  1. 移动端 Modal 组件开发杂谈

    Vant 是有赞开发的一套基于 Vue 2.0 的 Mobile 组件库,在开发的过程中也踩了很多坑,今天我们就来聊一聊开发一个移动端 Modal 组件(在有赞该组件被称为 Popup )需要注意的一 ...

  2. 利用bootstrap的modal组件自定义alert,confirm和modal对话框

    由于浏览器提供的alert和confirm框体验不好,而且浏览器没有提供一个标准的以对话框的形式显示自定义HTML的弹框函数,所以很多项目都会自定义对话框组件.本篇文章介绍自己在项目中基于bootst ...

  3. vue前端开发那些事——vue组件开发

    vue的学习曲线不是很陡(相比其它框架,如anglarjs),官方文档比较全面,分为基础篇和高级篇.我们刚开始学习的时候,肯定像引用jquery那样,先把vue的js引进来,然后学习基础内容.如果仅仅 ...

  4. React-Native 组件开发方法

    前言 React Native的开发思路是通过组合各种组件来组织整个App,在大部分情况下通过组合View.Image等几个基础的组件,可以非常方便的实现各种复杂的跨平台组件,不过在需要原生功能支持. ...

  5. 饿了么基于Vue2.0的通用组件开发之路(分享会记录)

    Element:一套通用组件库的开发之路 Element 是由饿了么UED设计.饿了么大前端开发的一套基于 Vue 2.0 的桌面端组件库.今天我们要分享的就是开发 Element 的一些心得. 官网 ...

  6. 基于vue的新组件开发

    前天完成了一个新组件的开发,做的过程也是各种遇到问题,彻底弄懂了slot,巩固了一些flex布局和jquery的知识,比起自己第一次做组件开发,现在已经是能够下手做,遇到问题解决问题,还算有进步. 但 ...

  7. winRT Com组件开发流程总结

    winRT Com组件开发: 1.编辑idl文件,winRT COM的idl文件与win32的idl文件有差异,如下: interface ItestWinRTClass; runtimeclass ...

  8. [js开源组件开发]localStorage-cache本地存储的缓存管理

    localStorage-cache本地存储的缓存管理 距离上次的组件开发有近三个月的时间了,最近一直在做一些杂事,无法静下心来写写代码,也是在学习emberjs,在emberjs中有一个很重要的东西 ...

  9. 【2015上半年总结】js开源组件开发系列索引

    js开源组件开发系列一索引 2015.8 by 田想兵 个人网站 从3月份进入新公司以来,时经五个月,我以平均每周1个小组件的速度,已经完成的js组件有22个之余了,已基本上全部用到实际项目中,这些小 ...

  10. [js开源组件开发]network异步请求ajax的扩展

    network异步请求ajax的扩展 在日常的应用中,你可能直接调用$.ajax是会有些问题的,比如说用户的重复点击,比如说我只希望它成功提交一次后就不能再提交,比如说我希望有个正在提交的loadin ...

随机推荐

  1. 深入理解 Java 修饰符与封装:访问权限、行为控制与数据隐藏

    Java 修饰符 Java 修饰符 用于控制类.属性.方法和构造函数的访问权限和行为.它们可以分为两组: 访问修饰符: public: 意味着代码对所有类可访问. private: 意味着代码只能在声 ...

  2. C# 方法详解:定义、调用、参数、默认值、返回值、命名参数、方法重载全解析

    C# Methods 方法是一段代码,只有在调用时才会运行. 您可以将数据(称为参数)传递给方法. 方法用于执行某些操作,也被称为函数. 为什么使用方法?为了重用代码:定义一次代码,然后多次使用. 创 ...

  3. openGauss 2.1.0 闪回特性

    openGauss 2.1.0 闪回特性 openGauss 2.1.0 于 2021 年 9 月 30 日发布,是 openGauss 的一个 Preview 版本,该版本生命周期仅为半年.该版本的 ...

  4. 无监督多视角行人检测 Unsupervised Multi-view Pedestrian Detection

    无监督多视角行人检测 Unsupervised Multi-view Pedestrian Detection 论文url:https://arxiv.org/abs/2305.12457 论文简述 ...

  5. WAF 原理入门

    WAF 入门 WAF 功能 WAF 全称叫 Web Application Firewall,和传统防火墙的区别是,它是工作在应用层的防火墙,主要对 web 请求/响应进行防护.那么 WAF 有什么功 ...

  6. redis 简单整理——redis 的字符串基本结构和命令[二]

    前言 简单介绍一下redis的基本结构和命令. 正文 redis有5种基本结构: 字符串 哈希 列表 集合 有序集合 那么就来看下其基本命令吧. 通用命令键: keys * 查看全部键,一般不怎么用. ...

  7. c# 优化代码的一些规则——用委托表示回调[五]

    前言 委托为什么可以作为回调? 因为委托可以作为方法的参数. 正文 通过委托,是一种定义类型安全回调. 记得第一个接触委托的时候,是老师讲的,后来真正用的是完成学期项目,一个winform,委托作为事 ...

  8. leetcode:94. 二叉树的中序遍历

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? ...

  9. huggingface vit训练CIFAR10数据集代码 ,可以改dataset训练自己的数据

    上代码,使用hugging face fineturn vit模型 自己写的代码 from transformers import ViTImageProcessor, ViTForImageClas ...

  10. 嘉楠k210 多线程 点亮流水灯

    from Maix import GPIO from fpioa_manager import fm import _thread import time fm.register(24, fm.fpi ...