Vue-函数式组件

最近在开发项目的时候,定制了一个公司内部样式的Modal模态框组件。

Modal组件伪代码

<!-- Modal/index.vue-->
<template><div class="modal-container" id="modalContainer"><!-- Modal Content --><div class="modal-content"><!-- Close Button --><span class="modal-close" @click="props.cancel">&times;</span><!-- Modal Header --><div class="modal-header">{{ props.title }}</div><!-- Modal Body --><div class="modal-body"><p>{{ props.content }}</p></div><!-- Modal Footer --><div class="modal-footer"><button class="button secondary" @click="props.cancel">{{ props.cancelButtonText }}</button><button class="button primary" @click="props.confirm">{{ props.confirmButtonText }}</button></div></div></div>
</template><script setup name="Modal">const props = defineProps({title: {type: String,default: 'Modal Title',},content: {type: String,default: 'This is the modal content.',},confirmButtonText: {type: String,default: 'Confirm',},cancelButtonText: {type: String,default: 'Cancel',},confirm: {type: Function,default: () => {},},cancel: {type: Function,default: () => {},},})
</script><style scoped>
.modal-container {position: fixed;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.5);z-index: 999;
}/* Styles for the modal content */
.modal-content {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);background-color: #fff;padding: 20px;border-radius: 4px;box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);width: 300px;
}/* Styles for the close button */
.modal-close {position: absolute;top: 10px;right: 10px;cursor: pointer;
}/* Styles for the modal header */
.modal-header {font-weight: bold;margin-bottom: 10px;
}/* Styles for the modal body */
.modal-body {margin-bottom: 20px;
}/* Styles for the modal footer */
.modal-footer {text-align: right;
}/* Example styles for buttons */
.button {padding: 8px 16px;border: none;border-radius: 4px;cursor: pointer;
}.primary {background-color: #1890ff;color: #fff;
}.secondary {background-color: #ccc;
}
</style>

使用组件

<template><button @click="toggleModal">Show Modal</button><Modal v-if="modalVisible" v-bind="modalProps"></Modal>
</template><script setup>import { ref } from 'vue'import Modal from '../components/Modal/index.vue'const modalProps = {title: '弹窗标题',content: '弹窗内容,弹窗内容',confirmButtonText: '确认',cancelButtonText: '取消',confirm() {console.log('confirm')toggleModal()},cancel() {console.log('cancel')toggleModal()}}const modalVisible = ref(false)function toggleModal() {modalVisible.value = !modalVisible.value}
</script>

调用成功
在这里插入图片描述

改造支持函数形式调用

由于使用频率很高,每次都引入组件再使用略显麻烦,于是决定改造一下,支持函数调用该Modal组件

  1. 新建js文件,导入Modal.vue(单文件组件实际上是一个JS对象)
  2. 使用vue提供的createApp实例化Modal.vue组件,第一个参数是组件,第二个参数是传递给组件的props
  3. 创建一个div标签并插入body,调用modal实例下的mount方法挂载到div上。
  4. 销毁Modal:调用unmount方法并移除div元素
// Modal/index.js
import { createApp } from 'vue'
import Modal from './index.vue'export function showModal(props = {}) {// 创建一个div并插入bodyconst div = document.createElement('div')document.body.appendChild(div)// 创建一个modal实例const modal = createApp(Modal, {...props,// 劫持取消事件,卸载组件cancel() {props.cancel()unMount()},// 劫持确认事件,卸载组件confirm() {props.confirm()unMount()}})// 挂载到div上modal.mount(div)// 卸载组件function unMount() {modal.unmount()div.remove()}
}

函数式调用

<template><div>函数式调用:<button @click="handleClick">Show Modal</button></div>
</template><script setup name="FunctionComponent">import { showModal } from '../components/Modal';const modalProps = {title: '弹窗标题',content: '弹窗内容,弹窗内容',confirmButtonText: '确认',cancelButtonText: '取消',confirm() {console.log('confirm')},cancel() {console.log('cancel')}}// 函数式调用function handleClick() {showModal(modalProps)}
</script>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/57588.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

总结七大排序!

排序总览 外部排序&#xff1a;依赖硬盘&#xff08;外部存储器&#xff09;进行的排序。对于数据集合的要求特别高&#xff0c;只能在特定场合下使用&#xff08;比如一个省的高考成绩排序&#xff09;。包括桶排序&#xff0c;基数排序&#xff0c;计数排序&#xff0c;都是o…

docker容器监控:Cadvisor+InfluxDB+Grafana的安装部署

目录 CadvisorInfluxDBGrafan安装部署 1、安装docker-ce 2、阿里云镜像加速器 3、下载组件镜像 4、创建自定义网络 5、创建influxdb容器 6、创建Cadvisor 容器 7、查看Cadvisor 容器&#xff1a; &#xff08;1&#xff09;准备测试镜像 &#xff08;2&#xff09;通…

深入解析微店详情API:提升电商平台的技术实力

了解微店详情API的基本概念和功能&#xff1a; 微店详情API是用于通过商品ID获取商品详情数据的接口。它提供了丰富的商品信息&#xff0c;包括商品名称、价格、描述、规格、图片等。我们将介绍API的请求和响应结构&#xff0c;以及常见的参数和返回字段。 最佳实践&#xff1…

【stm32】初识stm32—stm32环境的搭建

文章目录 &#x1f6f8;stm32资料分享&#x1f354;stm32是什么&#x1f384;具体过程&#x1f3f3;️‍&#x1f308;安装驱动&#x1f388;1&#x1f388;2 &#x1f3f3;️‍&#x1f308;建立Start文件夹 &#x1f6f8;stm32资料分享 我用夸克网盘分享了「STM32入门教程资料…

基于Spring Boot的医院预约挂号网站设计与实现(Java+spring boot+MySQL)

获取源码或者论文请私信博主 演示视频&#xff1a; 基于Spring Boot的医院预约挂号网站设计与实现&#xff08;Javaspring bootMySQL&#xff09; 使用技术&#xff1a; 前端&#xff1a;html css javascript jQuery ajax thymeleaf 微信小程序 后端&#xff1a;Java spring…

微服务 云原生:搭建 Harbor 私有镜像仓库

Harbor官网 写在文前&#xff1a; 本文中用到机器均为虚拟机 CentOS-7-x86_64-Minimal-2009 镜像。 基础设施要求 虚拟机配置达到最低要求即可&#xff0c;本次系统中使用 docker 24.0.4、docker-compose 1.29.2。docker 及 docker-compose 的安装可以参考上篇文章 微服务 &am…

MobPush iOS SDK iOS实时活动

开发工具&#xff1a;Xcode 功能需要: SwiftUI实现UI页面&#xff0c;iOS16.1以上系统使用 功能使用: 需应用为启动状态 功能说明 iOS16.1 系统支持实时活动功能&#xff0c;可以在锁定屏幕上实时获知各种事情的进展&#xff0c;MobPushSDK iOS 4.0.3版本已完成适配&#xf…

Photoshop 2023 25.0beta「Mac」

Photoshop 2023是一款专业图像处理软件&#xff0c;它主要用于图像编辑、合成和设计等方面。 Photoshop beta创新式填充的功能特色包括&#xff1a; 自动识别和删除对象&#xff1a;该功能可以自动识别图像中的对象&#xff0c;并用周围的图像填充空白部分&#xff0c;使图像看…

(杭电多校)2023“钉耙编程”中国大学生算法设计超级联赛(6)

1001 Count 当k在区间(1n)/2的左边时,如图,[1,k]和[n-k1,n]完全相同,所以就m^(n-k) 当k在区间(1n)/2的右边时,如图,[1,n-k1]和[k,n]完全相同,所以也是m^(n-k) 别忘了特判,当k等于n时,n-k为0,然后a1a1,a2a2,..anan,所以没什么限制,那么就是m^n AC代码&#xff1a; #includ…

多通道振弦数据记录仪应用于桥梁监测

随着城市化进程的加快&#xff0c;城市桥梁的数量不断增加。对于城市交通的保障作用&#xff0c;桥梁的重要性不言而喻。而为了保障桥梁的安全&#xff0c;桥梁监测的重要性也越来越受到人们的重视。在桥梁监测中&#xff0c;多通道振弦数据记录仪的应用得到了广泛的认可和应用…

什么是芯片?

https://zhuanlan.zhihu.com/p/228757435?utm_sourceweibo&utm_mediumsocial&utm_oi895441374156029952&utm_contentsnapshot 什么是芯片&#xff1f; 半导体&#xff1a;一种材料&#xff0c;导电性能介于导体和半导体之间&#xff0c;比如硅、锗、砷化镓。用这…

JMeter命令行执行+生成HTML报告

1、为什么用命令行模式 使用GUI方式启动jmeter&#xff0c;运行线程较多的测试时&#xff0c;会造成内存和CPU的大量消耗&#xff0c;导致客户机卡死&#xff1b; 所以一般采用的方式是在GUI模式下调整测试脚本&#xff0c;再用命令行模式执行&#xff1b; 命令行方式支持在…