当前位置: 首页 > news >正文

使用van-dialog二次封装微信小程序模态框

由于微信小程序的wx.showModal不支持富文本内容,无法实现更灵活的展示效果,故需要进行二次封装

实现思路:使用van-dialog以及微信小程序的rich-text实现

代码如下:

// index.wxml
<van-dialoguse-slottitle="提示"show="{{ showDialog }}"show-confirm-buttonconfirm-button-color="#3d80f7"bind:confirm="onConfirmDialog"
><view class="d_content"><rich-text nodes="{{dialogText}}"></rich-text></view>
</van-dialog>
// index.js
Page({
data: {showDialog: false,dialogText: "",confirmCallback: null,},onCloseDialog() {this.setData({ showDialog: false }, () => {wx.showTabBar({animation: true,});setTimeout(() => {this.setData({dialogText: "",confirmCallback: null,});}, 300);});},onShowDialog(type, msg, rMsg, fn) {switch (type) {case "model":this.setData({showDialog: true,dialogText: `<p  style="text-align: justify;"><span style="color: rgba(0, 0, 0, 0.5);">${msg}</span></p>`,confirmCallback: fn ? fn : null,},() => {wx.hideTabBar({animation: true,});});break;case "reject":let mArray = msg.split(rMsg);let text = `<p style="text-align: left;text-align: justify;"><span style="color: rgb(140, 140, 140);">${mArray[0]}</span><span style="color: #ff5858;">${rMsg}</span><span style="color: rgb(140, 140, 140);">${mArray[1]}</span></p>`;this.setData({showDialog: true,dialogText: text,confirmCallback: fn ? fn : null,},() => {wx.hideTabBar({animation: true,});});break;case "custom":this.setData({showDialog: true,dialogText: msg,confirmCallback: fn ? fn : null,},() => {wx.hideTabBar({animation: true,});});break;default:this.setData({showDialog: true,dialogText: msg,confirmCallback: fn ? fn : null,},() => {wx.hideTabBar({animation: true,});});break;}},onConfirmDialog() {wx.showTabBar({animation: true,});this.onCloseDialog();if (typeof this.data.confirmCallback === "function") {this.data.confirmCallback();}},
})
// index.json
{"usingComponents": {"van-dialog": "@vant/weapp/dialog/index"}
}
// index.wxss
.d_content {box-sizing: border-box;padding: 36rpx 46rpx;
}

使用方法

this.onShowDialog("model",`您已向:${subItem.name}申请权限,请等待管理员审核。`);
this.onShowDialog("reject",`您的申请被拒绝,原因为:${subItem.reason},请根据拒绝原因检查您的提交信息,点击确认继续。`,subItem.reason,() => {// ...});

本文仅提供一种思路,可能不是最优写法,欢迎大家讨论留言

http://www.lryc.cn/news/181311.html

相关文章:

  • 生鲜蔬果同城配送社区团购小程序商城的作用是什么
  • Unity实现设计模式——状态模式
  • 差分数组的应用技巧
  • 斯坦福数据挖掘教程·第三版》读书笔记(英文版)Chapter 10 Mining Social-Network Graphs
  • DFS:842. 排列数字
  • pytorch之nn.Conv1d详解
  • H5生成二维码
  • Three.js加载360全景图片/视频
  • 北大硕士7年嵌入式学习经验分享
  • 华为鸿蒙手表开发之动态生成二维码
  • 2023-09-28 monetdb-databae的概念和作用-分析
  • 2024级199管理类联考之数学基础(上篇)
  • RFID技术引领汽车零部件加工新时代
  • python中使用matplotlib绘图
  • Qt Creator 使用技巧
  • 来看看双阶段目标检测算法趴
  • python利用matplotlib绘图,对于中文和负号不显示,显示方框“口口”完美解决办法!!
  • 【数组及指针经典笔试题解析】
  • Transformer学习-self-attention
  • Spring Boot:利用JPA进行数据库的增改
  • 列表的增删改查和遍历
  • 获取网卡上的IP、网关及DNS信息,获取最佳路由,遍历路由表中的条目(附源码)
  • 保姆级 -- Zookeeper超详解
  • 【通意千问】大模型GitHub开源工程学习笔记(2)--使用Qwen进行推理的示例代码解析,及transformers的库使用
  • 从0开始python学习-23.selenium 常见鼠标的操作
  • 电气基础——电源、变压器、接触器、断路器、线缆
  • 步力宝科技爆款产品定位,开创智能物联网新商业
  • 凉鞋的 Unity 笔记 105. 第一个通识:编辑-测试 循环
  • Bug:elementUI样式不起作用、Vue引入组件报错not found等(Vue+ElementUI问题汇总)
  • 【大麦小米学量化】使用文心一言AI编写股票量化交易策略代码(含演示代码和进阶演示)