小程序-我封装了一个wx.showModal方法,但是感觉传参有点繁琐?

我封装了一个wx.showModal方法,但是感觉传参有点繁琐,请问有没有好的方式解决传参问题,谢谢🙏

class Tips {
  loading() {
    wx.showLoading({ mask: true })
  }

  hide(type, callback, {title, content, confirmText, cancelText, showCancel}) {
    wx.hideLoading({
      success: (res) => {
        if (type === 'Modal') {
          this.modal((result) => {
            return callback(result)
          }, { title, content, confirmText, cancelText, showCancel })
          return
        };
        if (type === 'Toast') {
          return this.toast(title)
        }
        return callback(res)
      },
    })
  }

  modal(callback, { title, content, confirmText, cancelText, showCancel }) {
    const obj = { title, content, confirmText, cancelText, showCancel };
    wx.showModal({
      title: obj.title || '提示',
      content: obj.content || '提示',
      confirmText: obj.confirmText || '确定',
      cancelText: obj.cancelText || '取消',
      showCancel: obj.showCancel || false,
      success: (res) => {
        return callback(res)
      }
    })
  }

  toast(title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 1500,
      mask: true,
    })
  }
};

const show = new Tips();
export default show

调用方法
参数传的顺序不对就会报错

show.hide('Modal', (res) => {
        const eventChannel = this.getOpenerEventChannel()
        if (res.confirm) {
          const result = this._pickClause();
          console.log("result => ", result);
          eventChannel.emit('acceptDataFromOpenedPage', { data: result });
          wx.navigateBack({
            delta: 1,
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }, { content, showCancel });
阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题