小程序篇 -- 制作一个弹窗组件
1、首先我们来创建弹窗的子组件,在components下面创建popup,代码如下:
popup.wxml
1 2 3 4 5 6 7 8 9 10 | <view class = "wx-popup" hidden= "{{flag}}" > <view class = 'popup-container' > <view class = "wx-popup-title" >{{title}}</view> <view class = "wx-popup-con" >{{content}}</view> <view class = "wx-popup-btn" > <text class = "btn-no" bindtap= '_error' >{{btn_no}}</text> <text class = "btn-ok" bindtap= '_success' >{{btn_ok}}</text> </view> </view> </view> |
popup.wxss
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | .wx-popup { position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, .5); } .popup-container { position: absolute; left: 50%; top: 50%; width: 80%; max-width: 600rpx; border: 2rpx solid #ccc; border-radius: 10rpx; box-sizing: bordre-box; transform: translate(-50%, -50%); overflow: hidden; background: #fff; } .wx-popup-title { width: 100%; padding: 20rpx; text-align: center; font-size: 40rpx; border-bottom: 2rpx solid red; } .wx-popup-con { margin: 60rpx 10rpx; text-align: center; } .wx-popup-btn { display: flex; justify-content: space-around; margin-bottom: 40rpx; } .wx-popup-btn text { display: flex; align-items: center; justify-content: center; width: 30%; height: 88rpx; border: 2rpx solid #ccc; border-radius: 88rpx; } |
popup.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | Component({ options: { multipleSlots: true // 在组件定义时的选项中启用多slot支持 }, /** * 组件的属性列表 */ properties: { title: { // 属性名 type: String, // 类型(必填),目前接受的类型包括:String, Number, Boolean, Object, Array, null(表示任意类型) value: '标题' // 属性初始值(可选),如果未指定则会根据类型选择一个 }, // 弹窗内容 content: { type: String, value: '内容' }, // 弹窗取消按钮文字 btn_no: { type: String, value: '取消' }, // 弹窗确认按钮文字 btn_ok: { type: String, value: '确定' } }, /** * 组件的初始数据 */ data: { flag: true , }, /** * 组件的方法列表 */ methods: { //隐藏弹框 hidePopup: function () { this .setData({ flag: ! this .data.flag }) }, //展示弹框 showPopup () { this .setData({ flag: ! this .data.flag }) }, /* * 内部私有方法建议以下划线开头 * triggerEvent 用于触发事件 */ _error () { //触发取消回调 this .triggerEvent( "error" ) }, _success () { //触发成功回调 this .triggerEvent( "success" ); } } }) |
2、在首页引入这个组件,在index.json中配置"usingComponents",代码如下:
1 2 3 4 5 | { "usingComponents" : { "popup" : "/components/popup/popup" } } |
index.wxml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!--index.wxml--> <view class = "container" > <view class = "userinfo" > <button bindtap= "showPopup" > 点我 </button> </view> <popup id= 'popup' title= '小组件' content= '学会了吗' btn_no= '没有' btn_ok= '学会了' bind:error= "_error" bind:success= "_success" > </popup> </view> |
index.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Page({ onReady: function () { //获得popup组件 this .popup = this .selectComponent( "#popup" ); }, showPopup() { this .popup.showPopup(); }, //取消事件 _error() { console.log( '你点击了取消' ); this .popup.hidePopup(); }, //确认事件 _success() { console.log( '你点击了确定' ); this .popup.hidePopup(); } }) |
运行效果如下:
1 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具