使用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,
              () => {
                // ...
              }
            );

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

相关推荐
流口水的兔子2 小时前
作为一个新手,如果让你去用【微信小程序通过BLE实现与设备通讯】,你会怎么做,
前端·物联网·微信小程序
一念杂记2 小时前
免费开源!微信小程序商城源码,快速搭建你的线上商城系统!
微信小程序·uni-app
张晓~183399481214 小时前
数字人源码部署流程分享--- PC+小程序融合方案
javascript·小程序·矩阵·aigc·文心一言·html5
The_era_achievs_hero6 小时前
微信小程序61~70
微信小程序·小程序
编程猪猪侠7 小时前
Taro+Vue3实现微信小程序富文本编辑器组件开发指南
vue.js·微信小程序·taro
汤姆yu17 小时前
基于微信小程序的学校招生系统
微信小程序·小程序·招生小程序
说私域1 天前
基于开源AI智能名片链动2+1模式的S2B2C商城小程序:门店私域流量与视频号直播融合的生态创新研究
人工智能·小程序·开源
说私域1 天前
传统微商困境与开源链动2+1模式、AI智能名片及S2B2C商城小程序的转型破局
人工智能·小程序·开源
一渊之隔1 天前
微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权
微信小程序·小程序
racerun1 天前
微信小程序如何实现再多个页面共享数据
微信小程序·小程序