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

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

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

代码如下:

javascript 复制代码
// index.wxml
<van-dialog
  use-slot
  title="提示"
  show="{{ showDialog }}"
  show-confirm-button
  confirm-button-color="#3d80f7"
  bind:confirm="onConfirmDialog"
>
  <view class="d_content">
    <rich-text nodes="{{dialogText}}"></rich-text>
  </view>
</van-dialog>
javascript 复制代码
// 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();
    }
  },
})
javascript 复制代码
// index.json
{
  "usingComponents": {
    "van-dialog": "@vant/weapp/dialog/index"
  }
}
javascript 复制代码
// index.wxss
.d_content {
  box-sizing: border-box;
  padding: 36rpx 46rpx;
}

使用方法

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

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

相关推荐
Dignity_呱1 小时前
如何在不发版时,实现小程序的 AB 测试?
前端·面试·微信小程序
说私域2 小时前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa10 小时前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_9159184117 小时前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售21 小时前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序
suncentwl1 天前
做一个答题pk小程序多少钱?
小程序·答题小程序·知识竞赛·答题pk软件
说私域1 天前
基于开源链动2+1模式AI智能名片S2B2C商城小程序的流量转化策略研究
人工智能·小程序
咸虾米_1 天前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
微信小程序·小程序·uniapp开发·小程序地图api
未来之窗软件服务1 天前
蔬菜批发小程序:生产商的数字化转型利器——仙盟创梦IDE
小程序·自动化·仙盟创梦ide·东方仙盟·蔬菜批发·批发系统
数据皮皮侠1 天前
最新上市公司业绩说明会文本数据(2017.02-2025.08)
大数据·数据库·人工智能·笔记·物联网·小程序·区块链