• 重复代码,应封装为函数进行调用


    // bad case 都有展示modal的逻辑,开发同学直接复制粘贴
    function handleA(msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#02BACC',
        success: (res) => {
          if (res.confirm) {
            doA();
           }
         },
      });
    }

    function handleB(msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#02BACC',
        success: (res) => {
          if (res.confirm) {
            doB();
           }
         },
      });
    }

    function handleC(msg) {
      wx.showModal({
        title: '提示',
        content: msg,
        showCancel: false,
        confirmText: '确定',
        confirmColor: '#02BACC',
        success: (res) => {
          if (res.confirm) {
            doC();
           }
         },
      });
    }

    解决方案,封装 showModal 函数。

    function showModal (msg) {
      return new Promise((resolve, reject) => {
        wx.showModal({
          title: '提示',
          content: msg,
          showCancel: false,
          confirmText: '确定',
          confirmColor: '#02BACC',
          success: (res) => {
            if (res.confirm) resolve()
          },
          fail: (err) => {
            reject(err)
          }
        })
      })
    }

    funtion handleA(msg) {
      showModal(msg).then(
        doA();
      ).catch(() => { catchHandler();})
    }

    funtion handleB(msg) {
      showModal(msg).then(
        doB();
      ).catch(() => { catchHandler();})
    }

    funtion handleC(msg) {
      showModal(msg).then(
        doC();
      ).catch(() => { catchHandler();})
    }
  • 相关阅读:
    要离职了。
    上海找工作经历
    1.6. 三基色LED
    1.5. 板载LED PWM模式
    1.4. 板载LED控制
    1.3. 硬件篇之IO口(视频连接)
    1.2 Hello World
    1.8. 数码管
    ESP32编译自己的micropython固件
    1.1 准备工作
  • 原文地址:https://www.cnblogs.com/hellofangfang/p/15206064.html
Copyright © 2020-2023  润新知